Statistics
| Branch: | Revision:

ffmpeg / libavformat / mpegtsenc.c @ e0c850a8

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