~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavformat/mpegtsenc.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * MPEG2 transport stream (aka DVB) mux
 
2
 * MPEG2 transport stream (aka DVB) muxer
3
3
 * Copyright (c) 2003 Fabrice Bellard.
4
4
 *
5
 
 * This library is free software; you can redistribute it and/or
 
5
 * This file is part of FFmpeg.
 
6
 *
 
7
 * FFmpeg is free software; you can redistribute it and/or
6
8
 * modify it under the terms of the GNU Lesser General Public
7
9
 * License as published by the Free Software Foundation; either
8
 
 * version 2 of the License, or (at your option) any later version.
 
10
 * version 2.1 of the License, or (at your option) any later version.
9
11
 *
10
 
 * This library is distributed in the hope that it will be useful,
 
12
 * FFmpeg is distributed in the hope that it will be useful,
11
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
15
 * Lesser General Public License for more details.
14
16
 *
15
17
 * You should have received a copy of the GNU Lesser General Public
16
 
 * License along with this library; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 * License along with FFmpeg; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
20
 */
19
21
#include "avformat.h"
20
 
 
 
22
#include "crc.h"
21
23
#include "mpegts.h"
22
24
 
23
25
/* write DVB SI sections */
24
26
 
25
 
static const uint32_t crc_table[256] = {
26
 
        0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b,
27
 
        0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
28
 
        0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7,
29
 
        0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
30
 
        0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3,
31
 
        0x709f7b7a, 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
32
 
        0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58, 0xbaea46ef,
33
 
        0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
34
 
        0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb,
35
 
        0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
36
 
        0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0,
37
 
        0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
38
 
        0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4,
39
 
        0x0808d07d, 0x0cc9cdca, 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
40
 
        0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08,
41
 
        0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
42
 
        0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc,
43
 
        0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
44
 
        0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, 0xe0b41de7, 0xe4750050,
45
 
        0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
46
 
        0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34,
47
 
        0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
48
 
        0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1,
49
 
        0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
50
 
        0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5,
51
 
        0x3f9b762c, 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
52
 
        0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e, 0xf5ee4bb9,
53
 
        0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
54
 
        0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd,
55
 
        0xcda1f604, 0xc960ebb3, 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
56
 
        0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71,
57
 
        0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
58
 
        0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2,
59
 
        0x470cdd2b, 0x43cdc09c, 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
60
 
        0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e,
61
 
        0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
62
 
        0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a,
63
 
        0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
64
 
        0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 0xe3a1cbc1, 0xe760d676,
65
 
        0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
66
 
        0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662,
67
 
        0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
68
 
        0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
69
 
};
70
 
 
71
 
unsigned int mpegts_crc32(const uint8_t *data, int len)
72
 
{
73
 
    register int i;
74
 
    unsigned int crc = 0xffffffff;
75
 
    
76
 
    for (i=0; i<len; i++)
77
 
        crc = (crc << 8) ^ crc_table[((crc >> 24) ^ *data++) & 0xff];
78
 
    
79
 
    return crc;
80
 
}
81
 
 
82
27
/*********************************************/
83
28
/* mpegts section writer */
84
29
 
90
35
} MpegTSSection;
91
36
 
92
37
/* NOTE: 4 bytes must be left at the end for the crc32 */
93
 
void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
 
38
static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
94
39
{
95
40
    unsigned int crc;
96
41
    unsigned char packet[TS_PACKET_SIZE];
98
43
    unsigned char *q;
99
44
    int first, b, len1, left;
100
45
 
101
 
    crc = mpegts_crc32(buf, len - 4);
 
46
    crc = bswap_32(av_crc(av_crc04C11DB7, -1, buf, len - 4));
102
47
    buf[len - 4] = (crc >> 24) & 0xff;
103
48
    buf[len - 3] = (crc >> 16) & 0xff;
104
49
    buf[len - 2] = (crc >> 8) & 0xff;
105
50
    buf[len - 1] = (crc) & 0xff;
106
 
    
 
51
 
107
52
    /* send each packet */
108
53
    buf_ptr = buf;
109
54
    while (len > 0) {
120
65
        if (first)
121
66
            *q++ = 0; /* 0 offset */
122
67
        len1 = TS_PACKET_SIZE - (q - packet);
123
 
        if (len1 > len) 
 
68
        if (len1 > len)
124
69
            len1 = len;
125
70
        memcpy(q, buf_ptr, len1);
126
71
        q += len1;
145
90
    *q_ptr = q;
146
91
}
147
92
 
148
 
int mpegts_write_section1(MpegTSSection *s, int tid, int id, 
 
93
static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
149
94
                          int version, int sec_num, int last_sec_num,
150
95
                          uint8_t *buf, int len)
151
96
{
152
97
    uint8_t section[1024], *q;
153
98
    unsigned int tot_len;
154
 
    
 
99
 
155
100
    tot_len = 3 + 5 + len + 4;
156
101
    /* check if not too big */
157
102
    if (tot_len > 1024)
165
110
    *q++ = sec_num;
166
111
    *q++ = last_sec_num;
167
112
    memcpy(q, buf, len);
168
 
    
 
113
 
169
114
    mpegts_write_section(s, section, tot_len);
170
115
    return 0;
171
116
}
190
135
/* we retransmit the SI info at this rate */
191
136
#define SDT_RETRANS_TIME 500
192
137
#define PAT_RETRANS_TIME 100
 
138
#define PCR_RETRANS_TIME 20
193
139
 
194
140
typedef struct MpegTSWriteStream {
 
141
    struct MpegTSService *service;
195
142
    int pid; /* stream associated pid */
196
143
    int cc;
197
144
    int payload_index;
201
148
 
202
149
typedef struct MpegTSService {
203
150
    MpegTSSection pmt; /* MPEG2 pmt table context */
204
 
    int pcr_pid;
205
151
    int sid;           /* service ID */
206
152
    char *name;
207
153
    char *provider_name;
 
154
    int pcr_pid;
 
155
    int pcr_packet_count;
 
156
    int pcr_packet_freq;
208
157
} MpegTSService;
209
158
 
210
159
typedef struct MpegTSWrite {
226
175
    MpegTSService *service;
227
176
    uint8_t data[1012], *q;
228
177
    int i;
229
 
    
 
178
 
230
179
    q = data;
231
180
    for(i = 0; i < ts->nb_services; i++) {
232
181
        service = ts->services[i];
254
203
    val = 0xf000 | (q - program_info_length_ptr - 2);
255
204
    program_info_length_ptr[0] = val >> 8;
256
205
    program_info_length_ptr[1] = val;
257
 
    
 
206
 
258
207
    for(i = 0; i < s->nb_streams; i++) {
259
208
        AVStream *st = s->streams[i];
260
209
        MpegTSWriteStream *ts_st = st->priv_data;
261
 
        switch(st->codec.codec_type) {
262
 
        case CODEC_TYPE_VIDEO:
263
 
            stream_type = STREAM_TYPE_VIDEO_MPEG2; //XXX/FIXME is this (and the define) correct?
264
 
            break;
265
 
        case CODEC_TYPE_AUDIO:
 
210
        switch(st->codec->codec_id) {
 
211
        case CODEC_ID_MPEG1VIDEO:
 
212
        case CODEC_ID_MPEG2VIDEO:
 
213
            stream_type = STREAM_TYPE_VIDEO_MPEG2;
 
214
            break;
 
215
        case CODEC_ID_MPEG4:
 
216
            stream_type = STREAM_TYPE_VIDEO_MPEG4;
 
217
            break;
 
218
        case CODEC_ID_H264:
 
219
            stream_type = STREAM_TYPE_VIDEO_H264;
 
220
            break;
 
221
        case CODEC_ID_MP2:
 
222
        case CODEC_ID_MP3:
266
223
            stream_type = STREAM_TYPE_AUDIO_MPEG1;
267
224
            break;
 
225
        case CODEC_ID_AAC:
 
226
            stream_type = STREAM_TYPE_AUDIO_AAC;
 
227
            break;
 
228
        case CODEC_ID_AC3:
 
229
            stream_type = STREAM_TYPE_AUDIO_AC3;
 
230
            break;
268
231
        default:
269
232
            stream_type = STREAM_TYPE_PRIVATE_DATA;
270
233
            break;
275
238
        q += 2; /* patched after */
276
239
 
277
240
        /* write optional descriptors here */
 
241
        switch(st->codec->codec_type) {
 
242
        case CODEC_TYPE_AUDIO:
 
243
            if (strlen(st->language) == 3) {
 
244
                *q++ = 0x0a; /* ISO 639 language descriptor */
 
245
                *q++ = 4;
 
246
                *q++ = st->language[0];
 
247
                *q++ = st->language[1];
 
248
                *q++ = st->language[2];
 
249
                *q++ = 0; /* undefined type */
 
250
            }
 
251
            break;
 
252
        case CODEC_TYPE_SUBTITLE:
 
253
            {
 
254
                const char *language;
 
255
                language = st->language;
 
256
                if (strlen(language) != 3)
 
257
                    language = "eng";
 
258
                *q++ = 0x59;
 
259
                *q++ = 8;
 
260
                *q++ = language[0];
 
261
                *q++ = language[1];
 
262
                *q++ = language[2];
 
263
                *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
 
264
                put16(&q, 1); /* page id */
 
265
                put16(&q, 1); /* ancillary page id */
 
266
            }
 
267
            break;
 
268
        }
278
269
 
279
270
        val = 0xf000 | (q - desc_length_ptr - 2);
280
271
        desc_length_ptr[0] = val >> 8;
282
273
    }
283
274
    mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
284
275
                          data, q - data);
285
 
}   
 
276
}
286
277
 
287
278
/* NOTE: str == NULL is accepted for an empty string */
288
279
static void putstr8(uint8_t **q_ptr, const char *str)
307
298
    MpegTSService *service;
308
299
    uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
309
300
    int i, running_status, free_ca_mode, val;
310
 
    
 
301
 
311
302
    q = data;
312
303
    put16(&q, ts->onid);
313
304
    *q++ = 0xff;
330
321
        desc_len_ptr[0] = q - desc_len_ptr - 1;
331
322
 
332
323
        /* fill descriptor length */
333
 
        val = (running_status << 13) | (free_ca_mode << 12) | 
 
324
        val = (running_status << 13) | (free_ca_mode << 12) |
334
325
            (q - desc_list_len_ptr - 2);
335
326
        desc_list_len_ptr[0] = val >> 8;
336
327
        desc_list_len_ptr[1] = val;
339
330
                          data, q - data);
340
331
}
341
332
 
342
 
static MpegTSService *mpegts_add_service(MpegTSWrite *ts, 
343
 
                                         int sid, 
344
 
                                         const char *provider_name, 
 
333
static MpegTSService *mpegts_add_service(MpegTSWrite *ts,
 
334
                                         int sid,
 
335
                                         const char *provider_name,
345
336
                                         const char *name)
346
337
{
347
338
    MpegTSService *service;
371
362
    MpegTSService *service;
372
363
    AVStream *st;
373
364
    int i, total_bit_rate;
 
365
    const char *service_name;
374
366
 
375
367
    ts->tsid = DEFAULT_TSID;
376
368
    ts->onid = DEFAULT_ONID;
377
369
    /* allocate a single DVB service */
378
 
    service = mpegts_add_service(ts, DEFAULT_SID, 
379
 
                                 DEFAULT_PROVIDER_NAME, DEFAULT_SERVICE_NAME);
 
370
    service_name = s->title;
 
371
    if (service_name[0] == '\0')
 
372
        service_name = DEFAULT_SERVICE_NAME;
 
373
    service = mpegts_add_service(ts, DEFAULT_SID,
 
374
                                 DEFAULT_PROVIDER_NAME, service_name);
380
375
    service->pmt.write_packet = section_write_packet;
381
376
    service->pmt.opaque = s;
382
377
 
398
393
        if (!ts_st)
399
394
            goto fail;
400
395
        st->priv_data = ts_st;
 
396
        ts_st->service = service;
401
397
        ts_st->pid = DEFAULT_START_PID + i;
402
398
        ts_st->payload_pts = AV_NOPTS_VALUE;
403
 
        /* update PCR pid if needed */
404
 
        if (st->codec.codec_type == CODEC_TYPE_VIDEO && 
 
399
        /* update PCR pid by using the first video stream */
 
400
        if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
405
401
            service->pcr_pid == 0x1fff)
406
402
            service->pcr_pid = ts_st->pid;
407
 
        total_bit_rate += st->codec.bit_rate;
408
 
    }
 
403
        total_bit_rate += st->codec->bit_rate;
 
404
    }
 
405
 
 
406
    /* if no video stream, use the first stream as PCR */
 
407
    if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
 
408
        ts_st = s->streams[0]->priv_data;
 
409
        service->pcr_pid = ts_st->pid;
 
410
    }
 
411
 
409
412
    if (total_bit_rate <= 8 * 1024)
410
413
        total_bit_rate = 8 * 1024;
411
 
    ts->sdt_packet_freq = (total_bit_rate * SDT_RETRANS_TIME) / 
412
 
        (TS_PACKET_SIZE * 8 * 1000);
413
 
    ts->pat_packet_freq = (total_bit_rate * PAT_RETRANS_TIME) / 
 
414
    service->pcr_packet_freq = (total_bit_rate * PCR_RETRANS_TIME) /
 
415
        (TS_PACKET_SIZE * 8 * 1000);
 
416
    ts->sdt_packet_freq = (total_bit_rate * SDT_RETRANS_TIME) /
 
417
        (TS_PACKET_SIZE * 8 * 1000);
 
418
    ts->pat_packet_freq = (total_bit_rate * PAT_RETRANS_TIME) /
414
419
        (TS_PACKET_SIZE * 8 * 1000);
415
420
#if 0
416
 
    printf("%d %d %d\n", 
 
421
    printf("%d %d %d\n",
417
422
           total_bit_rate, ts->sdt_packet_freq, ts->pat_packet_freq);
418
423
#endif
419
424
 
463
468
    MpegTSWriteStream *ts_st = st->priv_data;
464
469
    uint8_t buf[TS_PACKET_SIZE];
465
470
    uint8_t *q;
466
 
    int val, is_start, len, ts_len, header_len;
 
471
    int val, is_start, len, header_len, write_pcr, private_code;
 
472
    int afc_len, stuffing_len;
 
473
    int64_t pcr = -1; /* avoid warning */
467
474
 
468
475
    is_start = 1;
469
476
    while (payload_size > 0) {
470
477
        retransmit_si_info(s);
471
478
 
 
479
        write_pcr = 0;
 
480
        if (ts_st->pid == ts_st->service->pcr_pid) {
 
481
            ts_st->service->pcr_packet_count++;
 
482
            if (ts_st->service->pcr_packet_count >=
 
483
                ts_st->service->pcr_packet_freq) {
 
484
                ts_st->service->pcr_packet_count = 0;
 
485
                write_pcr = 1;
 
486
                /* XXX: this is incorrect, but at least we have a PCR
 
487
                   value */
 
488
                pcr = pts;
 
489
            }
 
490
        }
 
491
 
472
492
        /* prepare packet header */
473
493
        q = buf;
474
494
        *q++ = 0x47;
477
497
            val |= 0x40;
478
498
        *q++ = val;
479
499
        *q++ = ts_st->pid;
480
 
        *q++ = 0x10 | ts_st->cc;
 
500
        *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
481
501
        ts_st->cc = (ts_st->cc + 1) & 0xf;
 
502
        if (write_pcr) {
 
503
            *q++ = 7; /* AFC length */
 
504
            *q++ = 0x10; /* flags: PCR present */
 
505
            *q++ = pcr >> 25;
 
506
            *q++ = pcr >> 17;
 
507
            *q++ = pcr >> 9;
 
508
            *q++ = pcr >> 1;
 
509
            *q++ = (pcr & 1) << 7;
 
510
            *q++ = 0;
 
511
        }
482
512
        if (is_start) {
483
513
            /* write PES header */
484
514
            *q++ = 0x00;
485
515
            *q++ = 0x00;
486
516
            *q++ = 0x01;
487
 
            if (st->codec.codec_type == CODEC_TYPE_VIDEO)
 
517
            private_code = 0;
 
518
            if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
488
519
                *q++ = 0xe0;
489
 
            else
 
520
            } else if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
 
521
                       (st->codec->codec_id == CODEC_ID_MP2 ||
 
522
                        st->codec->codec_id == CODEC_ID_MP3)) {
490
523
                *q++ = 0xc0;
 
524
            } else {
 
525
                *q++ = 0xbd;
 
526
                if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
 
527
                    private_code = 0x20;
 
528
                }
 
529
            }
491
530
            if (pts != AV_NOPTS_VALUE)
492
531
                header_len = 8;
493
532
            else
494
533
                header_len = 3;
 
534
            if (private_code != 0)
 
535
                header_len++;
495
536
            len = payload_size + header_len;
496
537
            *q++ = len >> 8;
497
538
            *q++ = len;
498
 
            *q++ = 0x80;
 
539
            val = 0x80;
 
540
            /* data alignment indicator is required for subtitle data */
 
541
            if (st->codec->codec_type == CODEC_TYPE_SUBTITLE)
 
542
                val |= 0x04;
 
543
            *q++ = val;
499
544
            if (pts != AV_NOPTS_VALUE) {
500
545
                *q++ = 0x80; /* PTS only */
501
546
                *q++ = 0x05; /* header len */
502
 
                val = (0x02 << 4) | 
 
547
                val = (0x02 << 4) |
503
548
                    (((pts >> 30) & 0x07) << 1) | 1;
504
549
                *q++ = val;
505
550
                val = (((pts >> 15) & 0x7fff) << 1) | 1;
512
557
                *q++ = 0x00;
513
558
                *q++ = 0x00;
514
559
            }
 
560
            if (private_code != 0)
 
561
                *q++ = private_code;
515
562
            is_start = 0;
516
563
        }
517
 
        /* write header */
518
 
        ts_len = q - buf;
519
 
        put_buffer(&s->pb, buf, ts_len);
520
 
        /* write data */
521
 
        len = TS_PACKET_SIZE - ts_len;
 
564
        /* header size */
 
565
        header_len = q - buf;
 
566
        /* data len */
 
567
        len = TS_PACKET_SIZE - header_len;
522
568
        if (len > payload_size)
523
569
            len = payload_size;
524
 
        put_buffer(&s->pb, payload, len);
 
570
        stuffing_len = TS_PACKET_SIZE - header_len - len;
 
571
        if (stuffing_len > 0) {
 
572
            /* add stuffing with AFC */
 
573
            if (buf[3] & 0x20) {
 
574
                /* stuffing already present: increase its size */
 
575
                afc_len = buf[4] + 1;
 
576
                memmove(buf + 4 + afc_len + stuffing_len,
 
577
                        buf + 4 + afc_len,
 
578
                        header_len - (4 + afc_len));
 
579
                buf[4] += stuffing_len;
 
580
                memset(buf + 4 + afc_len, 0xff, stuffing_len);
 
581
            } else {
 
582
                /* add stuffing */
 
583
                memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
 
584
                buf[3] |= 0x20;
 
585
                buf[4] = stuffing_len - 1;
 
586
                if (stuffing_len >= 2) {
 
587
                    buf[5] = 0x00;
 
588
                    memset(buf + 6, 0xff, stuffing_len - 2);
 
589
                }
 
590
            }
 
591
        }
 
592
        memcpy(buf + TS_PACKET_SIZE - len, payload, len);
525
593
        payload += len;
526
594
        payload_size -= len;
527
 
        ts_len += len;
528
 
        /* stuffing */
529
 
        len = TS_PACKET_SIZE - ts_len;
530
 
        if (len > 0) {
531
 
            memset(buf, 0xff, len);
532
 
            put_buffer(&s->pb, buf, len);
533
 
        }
 
595
        put_buffer(&s->pb, buf, TS_PACKET_SIZE);
534
596
    }
535
597
    put_flush_packet(&s->pb);
536
598
}
537
599
 
538
 
static int mpegts_write_packet(AVFormatContext *s, int stream_index,
539
 
                               const uint8_t *buf, int size, int64_t pts1)
 
600
static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
540
601
{
541
 
    AVStream *st = s->streams[stream_index];
 
602
    AVStream *st = s->streams[pkt->stream_index];
 
603
    int size= pkt->size;
 
604
    uint8_t *buf= pkt->data;
542
605
    MpegTSWriteStream *ts_st = st->priv_data;
543
 
    int len;
544
 
 
 
606
    int len, max_payload_size;
 
607
 
 
608
    if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
 
609
        /* for subtitle, a single PES packet must be generated */
 
610
        mpegts_write_pes(s, st, buf, size, pkt->pts);
 
611
        return 0;
 
612
    }
 
613
 
 
614
    max_payload_size = DEFAULT_PES_PAYLOAD_SIZE;
545
615
    while (size > 0) {
546
 
        len = DEFAULT_PES_PAYLOAD_SIZE - ts_st->payload_index;
 
616
        len = max_payload_size - ts_st->payload_index;
547
617
        if (len > size)
548
618
            len = size;
549
619
        memcpy(ts_st->payload + ts_st->payload_index, buf, len);
551
621
        size -= len;
552
622
        ts_st->payload_index += len;
553
623
        if (ts_st->payload_pts == AV_NOPTS_VALUE)
554
 
            ts_st->payload_pts = pts1;
555
 
        if (ts_st->payload_index >= DEFAULT_PES_PAYLOAD_SIZE) {
 
624
            ts_st->payload_pts = pkt->pts;
 
625
        if (ts_st->payload_index >= max_payload_size) {
556
626
            mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
557
627
                             ts_st->payload_pts);
558
628
            ts_st->payload_pts = AV_NOPTS_VALUE;
580
650
        }
581
651
    }
582
652
    put_flush_packet(&s->pb);
583
 
        
 
653
 
584
654
    for(i = 0; i < ts->nb_services; i++) {
585
655
        service = ts->services[i];
586
656
        av_freep(&service->provider_name);
589
659
    }
590
660
    av_free(ts->services);
591
661
 
592
 
    for(i = 0; i < s->nb_streams; i++) {
593
 
        st = s->streams[i];
594
 
        av_free(st->priv_data);
595
 
    }
596
662
    return 0;
597
663
}
598
664
 
599
 
AVOutputFormat mpegts_mux = {
 
665
AVOutputFormat mpegts_muxer = {
600
666
    "mpegts",
601
667
    "MPEG2 transport stream format",
602
668
    "video/x-mpegts",