Statistics
| Branch: | Revision:

ffmpeg / libavformat / mpegtsenc.c @ d7fb5a18

History | View | Annotate | Download (22.2 KB)

1 5dbafeb7 Fabrice Bellard
/*
2 7fbde343 Aurelien Jacobs
 * MPEG2 transport stream (aka DVB) muxer
3 5dbafeb7 Fabrice Bellard
 * Copyright (c) 2003 Fabrice Bellard.
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 5dbafeb7 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 5dbafeb7 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 5dbafeb7 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 5dbafeb7 Fabrice Bellard
 */
21 245976da Diego Biurrun
22
#include "libavutil/crc.h"
23 5dbafeb7 Fabrice Bellard
#include "avformat.h"
24
#include "mpegts.h"
25
26
/* write DVB SI sections */
27
28
/*********************************************/
29
/* mpegts section writer */
30
31
typedef struct MpegTSSection {
32
    int pid;
33
    int cc;
34
    void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
35
    void *opaque;
36
} MpegTSSection;
37
38
/* NOTE: 4 bytes must be left at the end for the crc32 */
39 7b49ce2e Stefan Huehner
static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
40 5dbafeb7 Fabrice Bellard
{
41
    unsigned int crc;
42
    unsigned char packet[TS_PACKET_SIZE];
43
    const unsigned char *buf_ptr;
44
    unsigned char *q;
45
    int first, b, len1, left;
46
47 3abe5fbd Aurelien Jacobs
    crc = bswap_32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, buf, len - 4));
48 5dbafeb7 Fabrice Bellard
    buf[len - 4] = (crc >> 24) & 0xff;
49
    buf[len - 3] = (crc >> 16) & 0xff;
50
    buf[len - 2] = (crc >> 8) & 0xff;
51
    buf[len - 1] = (crc) & 0xff;
52 115329f1 Diego Biurrun
53 5dbafeb7 Fabrice Bellard
    /* send each packet */
54
    buf_ptr = buf;
55
    while (len > 0) {
56
        first = (buf == buf_ptr);
57
        q = packet;
58
        *q++ = 0x47;
59
        b = (s->pid >> 8);
60
        if (first)
61
            b |= 0x40;
62
        *q++ = b;
63
        *q++ = s->pid;
64
        *q++ = 0x10 | s->cc;
65 3654a16d Baptiste Coudurier
        s->cc = (s->cc + 1) & 0xf;
66 5dbafeb7 Fabrice Bellard
        if (first)
67
            *q++ = 0; /* 0 offset */
68
        len1 = TS_PACKET_SIZE - (q - packet);
69 115329f1 Diego Biurrun
        if (len1 > len)
70 5dbafeb7 Fabrice Bellard
            len1 = len;
71
        memcpy(q, buf_ptr, len1);
72
        q += len1;
73
        /* add known padding data */
74
        left = TS_PACKET_SIZE - (q - packet);
75
        if (left > 0)
76
            memset(q, 0xff, left);
77
78
        s->write_packet(s, packet);
79
80
        buf_ptr += len1;
81
        len -= len1;
82
    }
83
}
84
85
static inline void put16(uint8_t **q_ptr, int val)
86
{
87
    uint8_t *q;
88
    q = *q_ptr;
89
    *q++ = val >> 8;
90
    *q++ = val;
91
    *q_ptr = q;
92
}
93
94 7b49ce2e Stefan Huehner
static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
95 5dbafeb7 Fabrice Bellard
                          int version, int sec_num, int last_sec_num,
96
                          uint8_t *buf, int len)
97
{
98
    uint8_t section[1024], *q;
99
    unsigned int tot_len;
100 115329f1 Diego Biurrun
101 5dbafeb7 Fabrice Bellard
    tot_len = 3 + 5 + len + 4;
102
    /* check if not too big */
103
    if (tot_len > 1024)
104
        return -1;
105
106
    q = section;
107
    *q++ = tid;
108
    put16(&q, 0xb000 | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
109
    put16(&q, id);
110
    *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
111
    *q++ = sec_num;
112
    *q++ = last_sec_num;
113
    memcpy(q, buf, len);
114 115329f1 Diego Biurrun
115 5dbafeb7 Fabrice Bellard
    mpegts_write_section(s, section, tot_len);
116
    return 0;
117
}
118
119
/*********************************************/
120
/* mpegts writer */
121
122
#define DEFAULT_PMT_START_PID   0x1000
123
#define DEFAULT_START_PID       0x0100
124
#define DEFAULT_PROVIDER_NAME   "FFmpeg"
125
#define DEFAULT_SERVICE_NAME    "Service01"
126
127
/* default network id, transport stream and service identifiers */
128
#define DEFAULT_ONID            0x0001
129
#define DEFAULT_TSID            0x0001
130
#define DEFAULT_SID             0x0001
131
132
/* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
133
#define DEFAULT_PES_HEADER_FREQ 16
134 69ef9450 Fabrice Bellard
#define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
135 5dbafeb7 Fabrice Bellard
136
/* we retransmit the SI info at this rate */
137
#define SDT_RETRANS_TIME 500
138
#define PAT_RETRANS_TIME 100
139 8b475508 Fabrice Bellard
#define PCR_RETRANS_TIME 20
140 5dbafeb7 Fabrice Bellard
141
typedef struct MpegTSWriteStream {
142 8b475508 Fabrice Bellard
    struct MpegTSService *service;
143 5dbafeb7 Fabrice Bellard
    int pid; /* stream associated pid */
144
    int cc;
145 69ef9450 Fabrice Bellard
    int payload_index;
146
    int64_t payload_pts;
147 700b9711 Måns Rullgård
    int64_t payload_dts;
148 69ef9450 Fabrice Bellard
    uint8_t payload[DEFAULT_PES_PAYLOAD_SIZE];
149 5dbafeb7 Fabrice Bellard
} MpegTSWriteStream;
150
151
typedef struct MpegTSService {
152
    MpegTSSection pmt; /* MPEG2 pmt table context */
153
    int sid;           /* service ID */
154
    char *name;
155
    char *provider_name;
156 8b475508 Fabrice Bellard
    int pcr_pid;
157
    int pcr_packet_count;
158
    int pcr_packet_freq;
159 5dbafeb7 Fabrice Bellard
} MpegTSService;
160
161
typedef struct MpegTSWrite {
162
    MpegTSSection pat; /* MPEG2 pat table */
163
    MpegTSSection sdt; /* MPEG2 sdt table context */
164
    MpegTSService **services;
165
    int sdt_packet_count;
166
    int sdt_packet_freq;
167
    int pat_packet_count;
168
    int pat_packet_freq;
169
    int nb_services;
170
    int onid;
171
    int tsid;
172
} MpegTSWrite;
173
174
static void mpegts_write_pat(AVFormatContext *s)
175
{
176
    MpegTSWrite *ts = s->priv_data;
177
    MpegTSService *service;
178
    uint8_t data[1012], *q;
179
    int i;
180 115329f1 Diego Biurrun
181 5dbafeb7 Fabrice Bellard
    q = data;
182
    for(i = 0; i < ts->nb_services; i++) {
183
        service = ts->services[i];
184
        put16(&q, service->sid);
185
        put16(&q, 0xe000 | service->pmt.pid);
186
    }
187
    mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, 0, 0, 0,
188
                          data, q - data);
189
}
190
191
static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
192
{
193
    //    MpegTSWrite *ts = s->priv_data;
194
    uint8_t data[1012], *q, *desc_length_ptr, *program_info_length_ptr;
195
    int val, stream_type, i;
196
197
    q = data;
198
    put16(&q, 0xe000 | service->pcr_pid);
199
200
    program_info_length_ptr = q;
201
    q += 2; /* patched after */
202
203
    /* put program info here */
204
205
    val = 0xf000 | (q - program_info_length_ptr - 2);
206
    program_info_length_ptr[0] = val >> 8;
207
    program_info_length_ptr[1] = val;
208 115329f1 Diego Biurrun
209 5dbafeb7 Fabrice Bellard
    for(i = 0; i < s->nb_streams; i++) {
210
        AVStream *st = s->streams[i];
211
        MpegTSWriteStream *ts_st = st->priv_data;
212 01f4895c Michael Niedermayer
        switch(st->codec->codec_id) {
213 278de475 Måns Rullgård
        case CODEC_ID_MPEG1VIDEO:
214
        case CODEC_ID_MPEG2VIDEO:
215
            stream_type = STREAM_TYPE_VIDEO_MPEG2;
216 5dbafeb7 Fabrice Bellard
            break;
217 278de475 Måns Rullgård
        case CODEC_ID_MPEG4:
218
            stream_type = STREAM_TYPE_VIDEO_MPEG4;
219
            break;
220
        case CODEC_ID_H264:
221
            stream_type = STREAM_TYPE_VIDEO_H264;
222
            break;
223 f4bba201 Anuradha Suraparaju
        case CODEC_ID_DIRAC:
224
            stream_type = STREAM_TYPE_VIDEO_DIRAC;
225
            break;
226 278de475 Måns Rullgård
        case CODEC_ID_MP2:
227
        case CODEC_ID_MP3:
228 ce34182d Michael Niedermayer
            stream_type = STREAM_TYPE_AUDIO_MPEG1;
229 5dbafeb7 Fabrice Bellard
            break;
230 278de475 Måns Rullgård
        case CODEC_ID_AAC:
231
            stream_type = STREAM_TYPE_AUDIO_AAC;
232
            break;
233
        case CODEC_ID_AC3:
234
            stream_type = STREAM_TYPE_AUDIO_AC3;
235
            break;
236 5dbafeb7 Fabrice Bellard
        default:
237
            stream_type = STREAM_TYPE_PRIVATE_DATA;
238
            break;
239
        }
240
        *q++ = stream_type;
241
        put16(&q, 0xe000 | ts_st->pid);
242
        desc_length_ptr = q;
243
        q += 2; /* patched after */
244
245
        /* write optional descriptors here */
246 01f4895c Michael Niedermayer
        switch(st->codec->codec_type) {
247 8b475508 Fabrice Bellard
        case CODEC_TYPE_AUDIO:
248
            if (strlen(st->language) == 3) {
249
                *q++ = 0x0a; /* ISO 639 language descriptor */
250
                *q++ = 4;
251
                *q++ = st->language[0];
252
                *q++ = st->language[1];
253
                *q++ = st->language[2];
254
                *q++ = 0; /* undefined type */
255
            }
256
            break;
257
        case CODEC_TYPE_SUBTITLE:
258
            {
259
                const char *language;
260
                language = st->language;
261
                if (strlen(language) != 3)
262
                    language = "eng";
263
                *q++ = 0x59;
264
                *q++ = 8;
265
                *q++ = language[0];
266
                *q++ = language[1];
267
                *q++ = language[2];
268
                *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
269
                put16(&q, 1); /* page id */
270
                put16(&q, 1); /* ancillary page id */
271
            }
272
            break;
273 f4bba201 Anuradha Suraparaju
        case CODEC_TYPE_VIDEO:
274
            if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
275
                *q++ = 0x05; /*MPEG-2 registration descriptor*/
276
                *q++ = 4;
277
                *q++ = 'd';
278
                *q++ = 'r';
279
                *q++ = 'a';
280
                *q++ = 'c';
281
            }
282
            break;
283 8b475508 Fabrice Bellard
        }
284 5dbafeb7 Fabrice Bellard
285
        val = 0xf000 | (q - desc_length_ptr - 2);
286
        desc_length_ptr[0] = val >> 8;
287
        desc_length_ptr[1] = val;
288
    }
289
    mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
290
                          data, q - data);
291 115329f1 Diego Biurrun
}
292 5dbafeb7 Fabrice Bellard
293
/* NOTE: str == NULL is accepted for an empty string */
294
static void putstr8(uint8_t **q_ptr, const char *str)
295
{
296
    uint8_t *q;
297
    int len;
298
299
    q = *q_ptr;
300
    if (!str)
301
        len = 0;
302
    else
303
        len = strlen(str);
304
    *q++ = len;
305
    memcpy(q, str, len);
306
    q += len;
307
    *q_ptr = q;
308
}
309
310
static void mpegts_write_sdt(AVFormatContext *s)
311
{
312
    MpegTSWrite *ts = s->priv_data;
313
    MpegTSService *service;
314
    uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
315
    int i, running_status, free_ca_mode, val;
316 115329f1 Diego Biurrun
317 5dbafeb7 Fabrice Bellard
    q = data;
318
    put16(&q, ts->onid);
319
    *q++ = 0xff;
320
    for(i = 0; i < ts->nb_services; i++) {
321
        service = ts->services[i];
322
        put16(&q, service->sid);
323
        *q++ = 0xfc | 0x00; /* currently no EIT info */
324
        desc_list_len_ptr = q;
325
        q += 2;
326
        running_status = 4; /* running */
327
        free_ca_mode = 0;
328
329
        /* write only one descriptor for the service name and provider */
330
        *q++ = 0x48;
331
        desc_len_ptr = q;
332
        q++;
333
        *q++ = 0x01; /* digital television service */
334
        putstr8(&q, service->provider_name);
335
        putstr8(&q, service->name);
336
        desc_len_ptr[0] = q - desc_len_ptr - 1;
337
338
        /* fill descriptor length */
339 115329f1 Diego Biurrun
        val = (running_status << 13) | (free_ca_mode << 12) |
340 5dbafeb7 Fabrice Bellard
            (q - desc_list_len_ptr - 2);
341
        desc_list_len_ptr[0] = val >> 8;
342
        desc_list_len_ptr[1] = val;
343
    }
344
    mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, 0, 0, 0,
345
                          data, q - data);
346
}
347
348 115329f1 Diego Biurrun
static MpegTSService *mpegts_add_service(MpegTSWrite *ts,
349
                                         int sid,
350
                                         const char *provider_name,
351 5dbafeb7 Fabrice Bellard
                                         const char *name)
352
{
353
    MpegTSService *service;
354
355
    service = av_mallocz(sizeof(MpegTSService));
356
    if (!service)
357
        return NULL;
358
    service->pmt.pid = DEFAULT_PMT_START_PID + ts->nb_services - 1;
359
    service->sid = sid;
360
    service->provider_name = av_strdup(provider_name);
361
    service->name = av_strdup(name);
362
    service->pcr_pid = 0x1fff;
363
    dynarray_add(&ts->services, &ts->nb_services, service);
364
    return service;
365
}
366
367
static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
368
{
369
    AVFormatContext *ctx = s->opaque;
370 899681cd Björn Axelsson
    put_buffer(ctx->pb, packet, TS_PACKET_SIZE);
371 5dbafeb7 Fabrice Bellard
}
372
373
static int mpegts_write_header(AVFormatContext *s)
374
{
375
    MpegTSWrite *ts = s->priv_data;
376
    MpegTSWriteStream *ts_st;
377
    MpegTSService *service;
378
    AVStream *st;
379
    int i, total_bit_rate;
380 8b475508 Fabrice Bellard
    const char *service_name;
381 115329f1 Diego Biurrun
382 5dbafeb7 Fabrice Bellard
    ts->tsid = DEFAULT_TSID;
383
    ts->onid = DEFAULT_ONID;
384
    /* allocate a single DVB service */
385 8b475508 Fabrice Bellard
    service_name = s->title;
386
    if (service_name[0] == '\0')
387
        service_name = DEFAULT_SERVICE_NAME;
388 115329f1 Diego Biurrun
    service = mpegts_add_service(ts, DEFAULT_SID,
389 8b475508 Fabrice Bellard
                                 DEFAULT_PROVIDER_NAME, service_name);
390 5dbafeb7 Fabrice Bellard
    service->pmt.write_packet = section_write_packet;
391
    service->pmt.opaque = s;
392
393
    ts->pat.pid = PAT_PID;
394
    ts->pat.cc = 0;
395
    ts->pat.write_packet = section_write_packet;
396
    ts->pat.opaque = s;
397
398
    ts->sdt.pid = SDT_PID;
399
    ts->sdt.cc = 0;
400
    ts->sdt.write_packet = section_write_packet;
401
    ts->sdt.opaque = s;
402
403
    /* assign pids to each stream */
404
    total_bit_rate = 0;
405
    for(i = 0;i < s->nb_streams; i++) {
406
        st = s->streams[i];
407
        ts_st = av_mallocz(sizeof(MpegTSWriteStream));
408
        if (!ts_st)
409
            goto fail;
410
        st->priv_data = ts_st;
411 8b475508 Fabrice Bellard
        ts_st->service = service;
412 5dbafeb7 Fabrice Bellard
        ts_st->pid = DEFAULT_START_PID + i;
413 69ef9450 Fabrice Bellard
        ts_st->payload_pts = AV_NOPTS_VALUE;
414 700b9711 Måns Rullgård
        ts_st->payload_dts = AV_NOPTS_VALUE;
415 8b475508 Fabrice Bellard
        /* update PCR pid by using the first video stream */
416 115329f1 Diego Biurrun
        if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
417 5dbafeb7 Fabrice Bellard
            service->pcr_pid == 0x1fff)
418
            service->pcr_pid = ts_st->pid;
419 01f4895c Michael Niedermayer
        total_bit_rate += st->codec->bit_rate;
420 5dbafeb7 Fabrice Bellard
    }
421 115329f1 Diego Biurrun
422 8b475508 Fabrice Bellard
    /* if no video stream, use the first stream as PCR */
423
    if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
424
        ts_st = s->streams[0]->priv_data;
425
        service->pcr_pid = ts_st->pid;
426
    }
427
428 5dbafeb7 Fabrice Bellard
    if (total_bit_rate <= 8 * 1024)
429
        total_bit_rate = 8 * 1024;
430 115329f1 Diego Biurrun
    service->pcr_packet_freq = (total_bit_rate * PCR_RETRANS_TIME) /
431 8b475508 Fabrice Bellard
        (TS_PACKET_SIZE * 8 * 1000);
432 115329f1 Diego Biurrun
    ts->sdt_packet_freq = (total_bit_rate * SDT_RETRANS_TIME) /
433 5dbafeb7 Fabrice Bellard
        (TS_PACKET_SIZE * 8 * 1000);
434 115329f1 Diego Biurrun
    ts->pat_packet_freq = (total_bit_rate * PAT_RETRANS_TIME) /
435 5dbafeb7 Fabrice Bellard
        (TS_PACKET_SIZE * 8 * 1000);
436
#if 0
437 115329f1 Diego Biurrun
    printf("%d %d %d\n",
438 5dbafeb7 Fabrice Bellard
           total_bit_rate, ts->sdt_packet_freq, ts->pat_packet_freq);
439
#endif
440
441
    /* write info at the start of the file, so that it will be fast to
442
       find them */
443
    mpegts_write_sdt(s);
444
    mpegts_write_pat(s);
445
    for(i = 0; i < ts->nb_services; i++) {
446
        mpegts_write_pmt(s, ts->services[i]);
447
    }
448 899681cd Björn Axelsson
    put_flush_packet(s->pb);
449 5dbafeb7 Fabrice Bellard
450
    return 0;
451
452
 fail:
453
    for(i = 0;i < s->nb_streams; i++) {
454
        st = s->streams[i];
455
        av_free(st->priv_data);
456
    }
457
    return -1;
458
}
459
460
/* send SDT, PAT and PMT tables regulary */
461
static void retransmit_si_info(AVFormatContext *s)
462
{
463
    MpegTSWrite *ts = s->priv_data;
464
    int i;
465
466
    if (++ts->sdt_packet_count == ts->sdt_packet_freq) {
467
        ts->sdt_packet_count = 0;
468
        mpegts_write_sdt(s);
469
    }
470
    if (++ts->pat_packet_count == ts->pat_packet_freq) {
471
        ts->pat_packet_count = 0;
472
        mpegts_write_pat(s);
473
        for(i = 0; i < ts->nb_services; i++) {
474
            mpegts_write_pmt(s, ts->services[i]);
475
        }
476
    }
477
}
478
479 700b9711 Måns Rullgård
static void write_pts(uint8_t *q, int fourbits, int64_t pts)
480
{
481
    int val;
482
483
    val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
484
    *q++ = val;
485
    val = (((pts >> 15) & 0x7fff) << 1) | 1;
486
    *q++ = val >> 8;
487
    *q++ = val;
488
    val = (((pts) & 0x7fff) << 1) | 1;
489
    *q++ = val >> 8;
490
    *q++ = val;
491
}
492
493 69ef9450 Fabrice Bellard
/* NOTE: pes_data contains all the PES packet */
494
static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
495
                             const uint8_t *payload, int payload_size,
496 700b9711 Måns Rullgård
                             int64_t pts, int64_t dts)
497 5dbafeb7 Fabrice Bellard
{
498
    MpegTSWriteStream *ts_st = st->priv_data;
499 69ef9450 Fabrice Bellard
    uint8_t buf[TS_PACKET_SIZE];
500 5dbafeb7 Fabrice Bellard
    uint8_t *q;
501 700b9711 Måns Rullgård
    int val, is_start, len, header_len, write_pcr, private_code, flags;
502 8b475508 Fabrice Bellard
    int afc_len, stuffing_len;
503
    int64_t pcr = -1; /* avoid warning */
504 5dbafeb7 Fabrice Bellard
505 69ef9450 Fabrice Bellard
    is_start = 1;
506
    while (payload_size > 0) {
507
        retransmit_si_info(s);
508 5dbafeb7 Fabrice Bellard
509 8b475508 Fabrice Bellard
        write_pcr = 0;
510
        if (ts_st->pid == ts_st->service->pcr_pid) {
511
            ts_st->service->pcr_packet_count++;
512 115329f1 Diego Biurrun
            if (ts_st->service->pcr_packet_count >=
513 8b475508 Fabrice Bellard
                ts_st->service->pcr_packet_freq) {
514
                ts_st->service->pcr_packet_count = 0;
515
                write_pcr = 1;
516
                /* XXX: this is incorrect, but at least we have a PCR
517
                   value */
518
                pcr = pts;
519
            }
520
        }
521
522 69ef9450 Fabrice Bellard
        /* prepare packet header */
523
        q = buf;
524
        *q++ = 0x47;
525
        val = (ts_st->pid >> 8);
526
        if (is_start)
527
            val |= 0x40;
528
        *q++ = val;
529
        *q++ = ts_st->pid;
530 8b475508 Fabrice Bellard
        *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
531 69ef9450 Fabrice Bellard
        ts_st->cc = (ts_st->cc + 1) & 0xf;
532 8b475508 Fabrice Bellard
        if (write_pcr) {
533
            *q++ = 7; /* AFC length */
534
            *q++ = 0x10; /* flags: PCR present */
535
            *q++ = pcr >> 25;
536
            *q++ = pcr >> 17;
537
            *q++ = pcr >> 9;
538
            *q++ = pcr >> 1;
539
            *q++ = (pcr & 1) << 7;
540
            *q++ = 0;
541
        }
542 69ef9450 Fabrice Bellard
        if (is_start) {
543 f4bba201 Anuradha Suraparaju
            int pes_extension = 0;
544 69ef9450 Fabrice Bellard
            /* write PES header */
545
            *q++ = 0x00;
546
            *q++ = 0x00;
547
            *q++ = 0x01;
548 8b475508 Fabrice Bellard
            private_code = 0;
549 01f4895c Michael Niedermayer
            if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
550 f4bba201 Anuradha Suraparaju
                if (st->codec->codec_id == CODEC_ID_DIRAC) {
551
                    *q++ = 0xfd;
552
                } else
553
                    *q++ = 0xe0;
554 01f4895c Michael Niedermayer
            } else if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
555
                       (st->codec->codec_id == CODEC_ID_MP2 ||
556
                        st->codec->codec_id == CODEC_ID_MP3)) {
557 115329f1 Diego Biurrun
                *q++ = 0xc0;
558 8b475508 Fabrice Bellard
            } else {
559
                *q++ = 0xbd;
560 01f4895c Michael Niedermayer
                if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
561 8b475508 Fabrice Bellard
                    private_code = 0x20;
562
                }
563
            }
564 700b9711 Måns Rullgård
            header_len = 0;
565
            flags = 0;
566
            if (pts != AV_NOPTS_VALUE) {
567
                header_len += 5;
568
                flags |= 0x80;
569
            }
570
            if (dts != AV_NOPTS_VALUE) {
571
                header_len += 5;
572
                flags |= 0x40;
573
            }
574 f4bba201 Anuradha Suraparaju
            if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
575
                st->codec->codec_id == CODEC_ID_DIRAC) {
576
                /* set PES_extension_flag */
577
                pes_extension = 1;
578
                flags |= 0x01;
579
580
                /*
581
                * One byte for PES2 extension flag +
582
                * one byte for extension length +
583
                * one byte for extension id
584
                */
585
                header_len += 3;
586
            }
587 700b9711 Måns Rullgård
            len = payload_size + header_len + 3;
588 8b475508 Fabrice Bellard
            if (private_code != 0)
589 700b9711 Måns Rullgård
                len++;
590 69ef9450 Fabrice Bellard
            *q++ = len >> 8;
591
            *q++ = len;
592 8b475508 Fabrice Bellard
            val = 0x80;
593
            /* data alignment indicator is required for subtitle data */
594 01f4895c Michael Niedermayer
            if (st->codec->codec_type == CODEC_TYPE_SUBTITLE)
595 8b475508 Fabrice Bellard
                val |= 0x04;
596
            *q++ = val;
597 700b9711 Måns Rullgård
            *q++ = flags;
598
            *q++ = header_len;
599 69ef9450 Fabrice Bellard
            if (pts != AV_NOPTS_VALUE) {
600 700b9711 Måns Rullgård
                write_pts(q, flags >> 6, pts);
601
                q += 5;
602
            }
603
            if (dts != AV_NOPTS_VALUE) {
604
                write_pts(q, 1, dts);
605
                q += 5;
606 5dbafeb7 Fabrice Bellard
            }
607 f4bba201 Anuradha Suraparaju
            if (pes_extension && st->codec->codec_id == CODEC_ID_DIRAC) {
608
                flags = 0x01;  /* set PES_extension_flag_2 */
609
                *q++ = flags;
610
                *q++ = 0x80 | 0x01;  /* marker bit + extension length */
611
                /*
612
                * Set the stream id extension flag bit to 0 and
613
                * write the extended stream id
614
                */
615
                *q++ = 0x00 | 0x60;
616
            }
617 8b475508 Fabrice Bellard
            if (private_code != 0)
618
                *q++ = private_code;
619 69ef9450 Fabrice Bellard
            is_start = 0;
620 5dbafeb7 Fabrice Bellard
        }
621 8b475508 Fabrice Bellard
        /* header size */
622
        header_len = q - buf;
623
        /* data len */
624
        len = TS_PACKET_SIZE - header_len;
625 69ef9450 Fabrice Bellard
        if (len > payload_size)
626
            len = payload_size;
627 8b475508 Fabrice Bellard
        stuffing_len = TS_PACKET_SIZE - header_len - len;
628
        if (stuffing_len > 0) {
629
            /* add stuffing with AFC */
630
            if (buf[3] & 0x20) {
631
                /* stuffing already present: increase its size */
632
                afc_len = buf[4] + 1;
633
                memmove(buf + 4 + afc_len + stuffing_len,
634 115329f1 Diego Biurrun
                        buf + 4 + afc_len,
635 8b475508 Fabrice Bellard
                        header_len - (4 + afc_len));
636
                buf[4] += stuffing_len;
637
                memset(buf + 4 + afc_len, 0xff, stuffing_len);
638
            } else {
639
                /* add stuffing */
640
                memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
641
                buf[3] |= 0x20;
642
                buf[4] = stuffing_len - 1;
643
                if (stuffing_len >= 2) {
644
                    buf[5] = 0x00;
645
                    memset(buf + 6, 0xff, stuffing_len - 2);
646
                }
647
            }
648
        }
649
        memcpy(buf + TS_PACKET_SIZE - len, payload, len);
650 69ef9450 Fabrice Bellard
        payload += len;
651
        payload_size -= len;
652 899681cd Björn Axelsson
        put_buffer(s->pb, buf, TS_PACKET_SIZE);
653 5dbafeb7 Fabrice Bellard
    }
654 899681cd Björn Axelsson
    put_flush_packet(s->pb);
655 69ef9450 Fabrice Bellard
}
656
657 e928649b Michael Niedermayer
static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
658 69ef9450 Fabrice Bellard
{
659 e928649b Michael Niedermayer
    AVStream *st = s->streams[pkt->stream_index];
660
    int size= pkt->size;
661
    uint8_t *buf= pkt->data;
662 69ef9450 Fabrice Bellard
    MpegTSWriteStream *ts_st = st->priv_data;
663 8b475508 Fabrice Bellard
    int len, max_payload_size;
664 69ef9450 Fabrice Bellard
665 01f4895c Michael Niedermayer
    if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
666 8b475508 Fabrice Bellard
        /* for subtitle, a single PES packet must be generated */
667 700b9711 Måns Rullgård
        mpegts_write_pes(s, st, buf, size, pkt->pts, AV_NOPTS_VALUE);
668 8b475508 Fabrice Bellard
        return 0;
669
    }
670 115329f1 Diego Biurrun
671 67a38cee Anuradha Suraparaju
    if (st->codec->codec_id == CODEC_ID_DIRAC) {
672
        /* for Dirac, a single PES packet must be generated */
673
        mpegts_write_pes(s, st, buf, size, pkt->pts, pkt->dts);
674
        return 0;
675
    }
676 8b475508 Fabrice Bellard
    max_payload_size = DEFAULT_PES_PAYLOAD_SIZE;
677 69ef9450 Fabrice Bellard
    while (size > 0) {
678 8b475508 Fabrice Bellard
        len = max_payload_size - ts_st->payload_index;
679 69ef9450 Fabrice Bellard
        if (len > size)
680
            len = size;
681
        memcpy(ts_st->payload + ts_st->payload_index, buf, len);
682
        buf += len;
683
        size -= len;
684
        ts_st->payload_index += len;
685
        if (ts_st->payload_pts == AV_NOPTS_VALUE)
686 e928649b Michael Niedermayer
            ts_st->payload_pts = pkt->pts;
687 700b9711 Måns Rullgård
        if (ts_st->payload_dts == AV_NOPTS_VALUE)
688
            ts_st->payload_dts = pkt->dts;
689 8b475508 Fabrice Bellard
        if (ts_st->payload_index >= max_payload_size) {
690 69ef9450 Fabrice Bellard
            mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
691 700b9711 Måns Rullgård
                             ts_st->payload_pts, ts_st->payload_dts);
692 69ef9450 Fabrice Bellard
            ts_st->payload_pts = AV_NOPTS_VALUE;
693 700b9711 Måns Rullgård
            ts_st->payload_dts = AV_NOPTS_VALUE;
694 69ef9450 Fabrice Bellard
            ts_st->payload_index = 0;
695
        }
696
    }
697 5dbafeb7 Fabrice Bellard
    return 0;
698
}
699
700
static int mpegts_write_end(AVFormatContext *s)
701
{
702
    MpegTSWrite *ts = s->priv_data;
703
    MpegTSWriteStream *ts_st;
704
    MpegTSService *service;
705
    AVStream *st;
706
    int i;
707
708
    /* flush current packets */
709
    for(i = 0; i < s->nb_streams; i++) {
710
        st = s->streams[i];
711
        ts_st = st->priv_data;
712 69ef9450 Fabrice Bellard
        if (ts_st->payload_index > 0) {
713
            mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
714 700b9711 Måns Rullgård
                             ts_st->payload_pts, ts_st->payload_dts);
715 5dbafeb7 Fabrice Bellard
        }
716
    }
717 899681cd Björn Axelsson
    put_flush_packet(s->pb);
718 115329f1 Diego Biurrun
719 5dbafeb7 Fabrice Bellard
    for(i = 0; i < ts->nb_services; i++) {
720
        service = ts->services[i];
721
        av_freep(&service->provider_name);
722
        av_freep(&service->name);
723
        av_free(service);
724
    }
725
    av_free(ts->services);
726
727
    return 0;
728
}
729
730 d2a067d1 Måns Rullgård
AVOutputFormat mpegts_muxer = {
731 5dbafeb7 Fabrice Bellard
    "mpegts",
732 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
733 5dbafeb7 Fabrice Bellard
    "video/x-mpegts",
734 d3ad044a Baptiste Coudurier
    "ts,m2t",
735 5dbafeb7 Fabrice Bellard
    sizeof(MpegTSWrite),
736
    CODEC_ID_MP2,
737 69ef9450 Fabrice Bellard
    CODEC_ID_MPEG2VIDEO,
738 5dbafeb7 Fabrice Bellard
    mpegts_write_header,
739
    mpegts_write_packet,
740
    mpegts_write_end,
741
};