Statistics
| Branch: | Revision:

ffmpeg / libavformat / mpegtsenc.c @ 83c2bc7a

History | View | Annotate | Download (28.6 KB)

1 5dbafeb7 Fabrice Bellard
/*
2 7fbde343 Aurelien Jacobs
 * MPEG2 transport stream (aka DVB) muxer
3 406792e7 Diego Biurrun
 * Copyright (c) 2003 Fabrice Bellard
4 5dbafeb7 Fabrice Bellard
 *
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 6a5d31ac Diego Biurrun
#include "libavutil/bswap.h"
23 245976da Diego Biurrun
#include "libavutil/crc.h"
24 de34dc39 Baptiste Coudurier
#include "libavcodec/mpegvideo.h"
25 5dbafeb7 Fabrice Bellard
#include "avformat.h"
26
#include "mpegts.h"
27 8fdd542c Baptiste Coudurier
#include "adts.h"
28 5dbafeb7 Fabrice Bellard
29
/* write DVB SI sections */
30
31
/*********************************************/
32
/* mpegts section writer */
33
34
typedef struct MpegTSSection {
35
    int pid;
36
    int cc;
37
    void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
38
    void *opaque;
39
} MpegTSSection;
40
41 b5931348 Baptiste Coudurier
typedef struct MpegTSService {
42
    MpegTSSection pmt; /* MPEG2 pmt table context */
43
    int sid;           /* service ID */
44
    char *name;
45
    char *provider_name;
46
    int pcr_pid;
47
    int pcr_packet_count;
48 1aae3489 Niobos
    int pcr_packet_period;
49 b5931348 Baptiste Coudurier
} MpegTSService;
50
51
typedef struct MpegTSWrite {
52
    MpegTSSection pat; /* MPEG2 pat table */
53
    MpegTSSection sdt; /* MPEG2 sdt table context */
54
    MpegTSService **services;
55
    int sdt_packet_count;
56 1aae3489 Niobos
    int sdt_packet_period;
57 b5931348 Baptiste Coudurier
    int pat_packet_count;
58 1aae3489 Niobos
    int pat_packet_period;
59 b5931348 Baptiste Coudurier
    int nb_services;
60
    int onid;
61
    int tsid;
62
    uint64_t cur_pcr;
63 7082ea56 Baptiste Coudurier
    int mux_rate; ///< set to 1 when VBR
64 b5931348 Baptiste Coudurier
} MpegTSWrite;
65
66 5dbafeb7 Fabrice Bellard
/* NOTE: 4 bytes must be left at the end for the crc32 */
67 7b49ce2e Stefan Huehner
static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
68 5dbafeb7 Fabrice Bellard
{
69 b5931348 Baptiste Coudurier
    MpegTSWrite *ts = ((AVFormatContext*)s->opaque)->priv_data;
70 5dbafeb7 Fabrice Bellard
    unsigned int crc;
71
    unsigned char packet[TS_PACKET_SIZE];
72
    const unsigned char *buf_ptr;
73
    unsigned char *q;
74
    int first, b, len1, left;
75
76 3abe5fbd Aurelien Jacobs
    crc = bswap_32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, buf, len - 4));
77 5dbafeb7 Fabrice Bellard
    buf[len - 4] = (crc >> 24) & 0xff;
78
    buf[len - 3] = (crc >> 16) & 0xff;
79
    buf[len - 2] = (crc >> 8) & 0xff;
80
    buf[len - 1] = (crc) & 0xff;
81 115329f1 Diego Biurrun
82 5dbafeb7 Fabrice Bellard
    /* send each packet */
83
    buf_ptr = buf;
84
    while (len > 0) {
85
        first = (buf == buf_ptr);
86
        q = packet;
87
        *q++ = 0x47;
88
        b = (s->pid >> 8);
89
        if (first)
90
            b |= 0x40;
91
        *q++ = b;
92
        *q++ = s->pid;
93 3654a16d Baptiste Coudurier
        s->cc = (s->cc + 1) & 0xf;
94 8d819221 Yann Coupin
        *q++ = 0x10 | s->cc;
95 5dbafeb7 Fabrice Bellard
        if (first)
96
            *q++ = 0; /* 0 offset */
97
        len1 = TS_PACKET_SIZE - (q - packet);
98 115329f1 Diego Biurrun
        if (len1 > len)
99 5dbafeb7 Fabrice Bellard
            len1 = len;
100
        memcpy(q, buf_ptr, len1);
101
        q += len1;
102
        /* add known padding data */
103
        left = TS_PACKET_SIZE - (q - packet);
104
        if (left > 0)
105
            memset(q, 0xff, left);
106
107
        s->write_packet(s, packet);
108
109
        buf_ptr += len1;
110
        len -= len1;
111 b5931348 Baptiste Coudurier
112
        ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
113 5dbafeb7 Fabrice Bellard
    }
114
}
115
116
static inline void put16(uint8_t **q_ptr, int val)
117
{
118
    uint8_t *q;
119
    q = *q_ptr;
120
    *q++ = val >> 8;
121
    *q++ = val;
122
    *q_ptr = q;
123
}
124
125 7b49ce2e Stefan Huehner
static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
126 5dbafeb7 Fabrice Bellard
                          int version, int sec_num, int last_sec_num,
127
                          uint8_t *buf, int len)
128
{
129
    uint8_t section[1024], *q;
130
    unsigned int tot_len;
131 115329f1 Diego Biurrun
132 5dbafeb7 Fabrice Bellard
    tot_len = 3 + 5 + len + 4;
133
    /* check if not too big */
134
    if (tot_len > 1024)
135
        return -1;
136
137
    q = section;
138
    *q++ = tid;
139
    put16(&q, 0xb000 | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
140
    put16(&q, id);
141
    *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
142
    *q++ = sec_num;
143
    *q++ = last_sec_num;
144
    memcpy(q, buf, len);
145 115329f1 Diego Biurrun
146 5dbafeb7 Fabrice Bellard
    mpegts_write_section(s, section, tot_len);
147
    return 0;
148
}
149
150
/*********************************************/
151
/* mpegts writer */
152
153
#define DEFAULT_PMT_START_PID   0x1000
154
#define DEFAULT_START_PID       0x0100
155
#define DEFAULT_PROVIDER_NAME   "FFmpeg"
156
#define DEFAULT_SERVICE_NAME    "Service01"
157
158
/* default network id, transport stream and service identifiers */
159
#define DEFAULT_ONID            0x0001
160
#define DEFAULT_TSID            0x0001
161
#define DEFAULT_SID             0x0001
162
163
/* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
164
#define DEFAULT_PES_HEADER_FREQ 16
165 69ef9450 Fabrice Bellard
#define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
166 5dbafeb7 Fabrice Bellard
167
/* we retransmit the SI info at this rate */
168
#define SDT_RETRANS_TIME 500
169
#define PAT_RETRANS_TIME 100
170 8b475508 Fabrice Bellard
#define PCR_RETRANS_TIME 20
171 5dbafeb7 Fabrice Bellard
172
typedef struct MpegTSWriteStream {
173 8b475508 Fabrice Bellard
    struct MpegTSService *service;
174 5dbafeb7 Fabrice Bellard
    int pid; /* stream associated pid */
175
    int cc;
176 69ef9450 Fabrice Bellard
    int payload_index;
177 b69017af Baptiste Coudurier
    int first_pts_check; ///< first pts check needed
178 69ef9450 Fabrice Bellard
    int64_t payload_pts;
179 700b9711 Måns Rullgård
    int64_t payload_dts;
180 69ef9450 Fabrice Bellard
    uint8_t payload[DEFAULT_PES_PAYLOAD_SIZE];
181 8fdd542c Baptiste Coudurier
    ADTSContext *adts;
182 5dbafeb7 Fabrice Bellard
} MpegTSWriteStream;
183
184
static void mpegts_write_pat(AVFormatContext *s)
185
{
186
    MpegTSWrite *ts = s->priv_data;
187
    MpegTSService *service;
188
    uint8_t data[1012], *q;
189
    int i;
190 115329f1 Diego Biurrun
191 5dbafeb7 Fabrice Bellard
    q = data;
192
    for(i = 0; i < ts->nb_services; i++) {
193
        service = ts->services[i];
194
        put16(&q, service->sid);
195
        put16(&q, 0xe000 | service->pmt.pid);
196
    }
197
    mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, 0, 0, 0,
198
                          data, q - data);
199
}
200
201
static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
202
{
203
    //    MpegTSWrite *ts = s->priv_data;
204
    uint8_t data[1012], *q, *desc_length_ptr, *program_info_length_ptr;
205
    int val, stream_type, i;
206
207
    q = data;
208
    put16(&q, 0xe000 | service->pcr_pid);
209
210
    program_info_length_ptr = q;
211
    q += 2; /* patched after */
212
213
    /* put program info here */
214
215
    val = 0xf000 | (q - program_info_length_ptr - 2);
216
    program_info_length_ptr[0] = val >> 8;
217
    program_info_length_ptr[1] = val;
218 115329f1 Diego Biurrun
219 5dbafeb7 Fabrice Bellard
    for(i = 0; i < s->nb_streams; i++) {
220
        AVStream *st = s->streams[i];
221
        MpegTSWriteStream *ts_st = st->priv_data;
222 4b358c3e Aurelien Jacobs
        AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL,0);
223 01f4895c Michael Niedermayer
        switch(st->codec->codec_id) {
224 278de475 Måns Rullgård
        case CODEC_ID_MPEG1VIDEO:
225
        case CODEC_ID_MPEG2VIDEO:
226
            stream_type = STREAM_TYPE_VIDEO_MPEG2;
227 5dbafeb7 Fabrice Bellard
            break;
228 278de475 Måns Rullgård
        case CODEC_ID_MPEG4:
229
            stream_type = STREAM_TYPE_VIDEO_MPEG4;
230
            break;
231
        case CODEC_ID_H264:
232
            stream_type = STREAM_TYPE_VIDEO_H264;
233
            break;
234 f4bba201 Anuradha Suraparaju
        case CODEC_ID_DIRAC:
235
            stream_type = STREAM_TYPE_VIDEO_DIRAC;
236
            break;
237 278de475 Måns Rullgård
        case CODEC_ID_MP2:
238
        case CODEC_ID_MP3:
239 ce34182d Michael Niedermayer
            stream_type = STREAM_TYPE_AUDIO_MPEG1;
240 5dbafeb7 Fabrice Bellard
            break;
241 278de475 Måns Rullgård
        case CODEC_ID_AAC:
242
            stream_type = STREAM_TYPE_AUDIO_AAC;
243
            break;
244
        case CODEC_ID_AC3:
245 a1f42882 Baptiste Coudurier
            stream_type = STREAM_TYPE_AUDIO_AC3;
246 278de475 Måns Rullgård
            break;
247 5dbafeb7 Fabrice Bellard
        default:
248
            stream_type = STREAM_TYPE_PRIVATE_DATA;
249
            break;
250
        }
251
        *q++ = stream_type;
252
        put16(&q, 0xe000 | ts_st->pid);
253
        desc_length_ptr = q;
254
        q += 2; /* patched after */
255
256
        /* write optional descriptors here */
257 01f4895c Michael Niedermayer
        switch(st->codec->codec_type) {
258 8b475508 Fabrice Bellard
        case CODEC_TYPE_AUDIO:
259 4b358c3e Aurelien Jacobs
            if (lang && strlen(lang->value) == 3) {
260 8b475508 Fabrice Bellard
                *q++ = 0x0a; /* ISO 639 language descriptor */
261
                *q++ = 4;
262 4b358c3e Aurelien Jacobs
                *q++ = lang->value[0];
263
                *q++ = lang->value[1];
264
                *q++ = lang->value[2];
265 8b475508 Fabrice Bellard
                *q++ = 0; /* undefined type */
266
            }
267
            break;
268
        case CODEC_TYPE_SUBTITLE:
269
            {
270
                const char *language;
271 4b358c3e Aurelien Jacobs
                language = lang && strlen(lang->value)==3 ? lang->value : "eng";
272 8b475508 Fabrice Bellard
                *q++ = 0x59;
273
                *q++ = 8;
274
                *q++ = language[0];
275
                *q++ = language[1];
276
                *q++ = language[2];
277
                *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
278
                put16(&q, 1); /* page id */
279
                put16(&q, 1); /* ancillary page id */
280
            }
281
            break;
282 f4bba201 Anuradha Suraparaju
        case CODEC_TYPE_VIDEO:
283
            if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
284
                *q++ = 0x05; /*MPEG-2 registration descriptor*/
285
                *q++ = 4;
286
                *q++ = 'd';
287
                *q++ = 'r';
288
                *q++ = 'a';
289
                *q++ = 'c';
290
            }
291
            break;
292 8b475508 Fabrice Bellard
        }
293 5dbafeb7 Fabrice Bellard
294
        val = 0xf000 | (q - desc_length_ptr - 2);
295
        desc_length_ptr[0] = val >> 8;
296
        desc_length_ptr[1] = val;
297
    }
298
    mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
299
                          data, q - data);
300 115329f1 Diego Biurrun
}
301 5dbafeb7 Fabrice Bellard
302
/* NOTE: str == NULL is accepted for an empty string */
303
static void putstr8(uint8_t **q_ptr, const char *str)
304
{
305
    uint8_t *q;
306
    int len;
307
308
    q = *q_ptr;
309
    if (!str)
310
        len = 0;
311
    else
312
        len = strlen(str);
313
    *q++ = len;
314
    memcpy(q, str, len);
315
    q += len;
316
    *q_ptr = q;
317
}
318
319
static void mpegts_write_sdt(AVFormatContext *s)
320
{
321
    MpegTSWrite *ts = s->priv_data;
322
    MpegTSService *service;
323
    uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
324
    int i, running_status, free_ca_mode, val;
325 115329f1 Diego Biurrun
326 5dbafeb7 Fabrice Bellard
    q = data;
327
    put16(&q, ts->onid);
328
    *q++ = 0xff;
329
    for(i = 0; i < ts->nb_services; i++) {
330
        service = ts->services[i];
331
        put16(&q, service->sid);
332
        *q++ = 0xfc | 0x00; /* currently no EIT info */
333
        desc_list_len_ptr = q;
334
        q += 2;
335
        running_status = 4; /* running */
336
        free_ca_mode = 0;
337
338
        /* write only one descriptor for the service name and provider */
339
        *q++ = 0x48;
340
        desc_len_ptr = q;
341
        q++;
342
        *q++ = 0x01; /* digital television service */
343
        putstr8(&q, service->provider_name);
344
        putstr8(&q, service->name);
345
        desc_len_ptr[0] = q - desc_len_ptr - 1;
346
347
        /* fill descriptor length */
348 115329f1 Diego Biurrun
        val = (running_status << 13) | (free_ca_mode << 12) |
349 5dbafeb7 Fabrice Bellard
            (q - desc_list_len_ptr - 2);
350
        desc_list_len_ptr[0] = val >> 8;
351
        desc_list_len_ptr[1] = val;
352
    }
353
    mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, 0, 0, 0,
354
                          data, q - data);
355
}
356
357 115329f1 Diego Biurrun
static MpegTSService *mpegts_add_service(MpegTSWrite *ts,
358
                                         int sid,
359
                                         const char *provider_name,
360 5dbafeb7 Fabrice Bellard
                                         const char *name)
361
{
362
    MpegTSService *service;
363
364
    service = av_mallocz(sizeof(MpegTSService));
365
    if (!service)
366
        return NULL;
367
    service->pmt.pid = DEFAULT_PMT_START_PID + ts->nb_services - 1;
368
    service->sid = sid;
369
    service->provider_name = av_strdup(provider_name);
370
    service->name = av_strdup(name);
371
    service->pcr_pid = 0x1fff;
372
    dynarray_add(&ts->services, &ts->nb_services, service);
373
    return service;
374
}
375
376
static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
377
{
378
    AVFormatContext *ctx = s->opaque;
379 899681cd Björn Axelsson
    put_buffer(ctx->pb, packet, TS_PACKET_SIZE);
380 5dbafeb7 Fabrice Bellard
}
381
382
static int mpegts_write_header(AVFormatContext *s)
383
{
384
    MpegTSWrite *ts = s->priv_data;
385
    MpegTSWriteStream *ts_st;
386
    MpegTSService *service;
387 7082ea56 Baptiste Coudurier
    AVStream *st, *pcr_st = NULL;
388 4b358c3e Aurelien Jacobs
    AVMetadataTag *title;
389 7082ea56 Baptiste Coudurier
    int i;
390 8b475508 Fabrice Bellard
    const char *service_name;
391 115329f1 Diego Biurrun
392 5dbafeb7 Fabrice Bellard
    ts->tsid = DEFAULT_TSID;
393
    ts->onid = DEFAULT_ONID;
394
    /* allocate a single DVB service */
395 4b358c3e Aurelien Jacobs
    title = av_metadata_get(s->metadata, "title", NULL, 0);
396
    service_name = title ? title->value : DEFAULT_SERVICE_NAME;
397 115329f1 Diego Biurrun
    service = mpegts_add_service(ts, DEFAULT_SID,
398 8b475508 Fabrice Bellard
                                 DEFAULT_PROVIDER_NAME, service_name);
399 5dbafeb7 Fabrice Bellard
    service->pmt.write_packet = section_write_packet;
400
    service->pmt.opaque = s;
401 677a1144 Baptiste Coudurier
    service->pmt.cc = 15;
402 5dbafeb7 Fabrice Bellard
403
    ts->pat.pid = PAT_PID;
404 8d819221 Yann Coupin
    ts->pat.cc = 15; // Initialize at 15 so that it wraps and be equal to 0 for the first packet we write
405 5dbafeb7 Fabrice Bellard
    ts->pat.write_packet = section_write_packet;
406
    ts->pat.opaque = s;
407
408
    ts->sdt.pid = SDT_PID;
409 8d819221 Yann Coupin
    ts->sdt.cc = 15;
410 5dbafeb7 Fabrice Bellard
    ts->sdt.write_packet = section_write_packet;
411
    ts->sdt.opaque = s;
412
413
    /* assign pids to each stream */
414
    for(i = 0;i < s->nb_streams; i++) {
415
        st = s->streams[i];
416
        ts_st = av_mallocz(sizeof(MpegTSWriteStream));
417
        if (!ts_st)
418
            goto fail;
419
        st->priv_data = ts_st;
420 8b475508 Fabrice Bellard
        ts_st->service = service;
421 5dbafeb7 Fabrice Bellard
        ts_st->pid = DEFAULT_START_PID + i;
422 69ef9450 Fabrice Bellard
        ts_st->payload_pts = AV_NOPTS_VALUE;
423 700b9711 Måns Rullgård
        ts_st->payload_dts = AV_NOPTS_VALUE;
424 b69017af Baptiste Coudurier
        ts_st->first_pts_check = 1;
425 d73a458f Baptiste Coudurier
        ts_st->cc = 15;
426 8b475508 Fabrice Bellard
        /* update PCR pid by using the first video stream */
427 115329f1 Diego Biurrun
        if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
428 7082ea56 Baptiste Coudurier
            service->pcr_pid == 0x1fff) {
429 5dbafeb7 Fabrice Bellard
            service->pcr_pid = ts_st->pid;
430 7082ea56 Baptiste Coudurier
            pcr_st = st;
431 9deba199 Niobos
        }
432 8fdd542c Baptiste Coudurier
        if (st->codec->codec_id == CODEC_ID_AAC &&
433
            st->codec->extradata_size > 0) {
434
            ts_st->adts = av_mallocz(sizeof(*ts_st->adts));
435
            if (!ts_st->adts)
436
                return AVERROR_NOMEM;
437
            if (ff_adts_decode_extradata(s, ts_st->adts, st->codec->extradata,
438
                                         st->codec->extradata_size) < 0)
439
                return -1;
440
        }
441 5dbafeb7 Fabrice Bellard
    }
442 115329f1 Diego Biurrun
443 8b475508 Fabrice Bellard
    /* if no video stream, use the first stream as PCR */
444
    if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
445 7082ea56 Baptiste Coudurier
        pcr_st = s->streams[0];
446
        ts_st = pcr_st->priv_data;
447 8b475508 Fabrice Bellard
        service->pcr_pid = ts_st->pid;
448
    }
449
450 7082ea56 Baptiste Coudurier
    ts->mux_rate = s->mux_rate ? s->mux_rate : 1;
451 3d0a94f6 Baptiste Coudurier
452 7082ea56 Baptiste Coudurier
    if (ts->mux_rate > 1) {
453 e3433702 Baptiste Coudurier
        service->pcr_packet_period = (ts->mux_rate * PCR_RETRANS_TIME) /
454
            (TS_PACKET_SIZE * 8 * 1000);
455
        ts->sdt_packet_period      = (ts->mux_rate * SDT_RETRANS_TIME) /
456
            (TS_PACKET_SIZE * 8 * 1000);
457
        ts->pat_packet_period      = (ts->mux_rate * PAT_RETRANS_TIME) /
458
            (TS_PACKET_SIZE * 8 * 1000);
459
460
        ts->cur_pcr = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
461 7082ea56 Baptiste Coudurier
    } else {
462
        /* Arbitrary values, PAT/PMT could be written on key frames */
463
        ts->sdt_packet_period = 200;
464
        ts->pat_packet_period = 40;
465
        if (pcr_st->codec->codec_type == CODEC_TYPE_AUDIO) {
466
            if (!pcr_st->codec->frame_size) {
467
                av_log(s, AV_LOG_WARNING, "frame size not set\n");
468
                service->pcr_packet_period =
469
                    pcr_st->codec->sample_rate/(10*512);
470
            } else {
471
                service->pcr_packet_period =
472
                    pcr_st->codec->sample_rate/(10*pcr_st->codec->frame_size);
473
            }
474
        } else {
475
            // max delta PCR 0.1s
476
            service->pcr_packet_period =
477
                pcr_st->codec->time_base.den/(10*pcr_st->codec->time_base.num);
478
        }
479
    }
480
481 6b18a3f5 Mike Scheutzow
    // output a PCR as soon as possible
482
    service->pcr_packet_count = service->pcr_packet_period;
483 7082ea56 Baptiste Coudurier
    ts->pat_packet_count = ts->pat_packet_period-1;
484
    ts->sdt_packet_count = ts->sdt_packet_period-1;
485 6b18a3f5 Mike Scheutzow
486 7082ea56 Baptiste Coudurier
    av_log(s, AV_LOG_INFO,
487
           "muxrate %d bps, pcr every %d pkts, "
488 907d9166 Mike Scheutzow
           "sdt every %d, pat/pmt every %d pkts\n",
489 7082ea56 Baptiste Coudurier
           ts->mux_rate, service->pcr_packet_period,
490
           ts->sdt_packet_period, ts->pat_packet_period);
491 907d9166 Mike Scheutzow
492 3d0a94f6 Baptiste Coudurier
493 899681cd Björn Axelsson
    put_flush_packet(s->pb);
494 5dbafeb7 Fabrice Bellard
495
    return 0;
496
497
 fail:
498
    for(i = 0;i < s->nb_streams; i++) {
499
        st = s->streams[i];
500
        av_free(st->priv_data);
501
    }
502
    return -1;
503
}
504
505
/* send SDT, PAT and PMT tables regulary */
506
static void retransmit_si_info(AVFormatContext *s)
507
{
508
    MpegTSWrite *ts = s->priv_data;
509
    int i;
510
511 1aae3489 Niobos
    if (++ts->sdt_packet_count == ts->sdt_packet_period) {
512 5dbafeb7 Fabrice Bellard
        ts->sdt_packet_count = 0;
513
        mpegts_write_sdt(s);
514
    }
515 1aae3489 Niobos
    if (++ts->pat_packet_count == ts->pat_packet_period) {
516 5dbafeb7 Fabrice Bellard
        ts->pat_packet_count = 0;
517
        mpegts_write_pat(s);
518
        for(i = 0; i < ts->nb_services; i++) {
519
            mpegts_write_pmt(s, ts->services[i]);
520
        }
521
    }
522
}
523
524 4df3bbbc Mike Scheutzow
/* Write a single null transport stream packet */
525
static void mpegts_insert_null_packet(AVFormatContext *s)
526
{
527
    MpegTSWrite *ts = s->priv_data;
528
    uint8_t *q;
529
    uint8_t buf[TS_PACKET_SIZE];
530
531
    q = buf;
532
    *q++ = 0x47;
533
    *q++ = 0x00 | 0x1f;
534
    *q++ = 0xff;
535
    *q++ = 0x10;
536
    memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
537
    put_buffer(s->pb, buf, TS_PACKET_SIZE);
538
    ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
539
}
540
541
/* Write a single transport stream packet with a PCR and no payload */
542
static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
543
{
544
    MpegTSWrite *ts = s->priv_data;
545
    MpegTSWriteStream *ts_st = st->priv_data;
546
    uint8_t *q;
547
    uint64_t pcr = ts->cur_pcr;
548
    uint8_t buf[TS_PACKET_SIZE];
549
550
    q = buf;
551
    *q++ = 0x47;
552
    *q++ = ts_st->pid >> 8;
553
    *q++ = ts_st->pid;
554
    *q++ = 0x20 | ts_st->cc;   /* Adaptation only */
555
    /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
556
    *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
557
    *q++ = 0x10;               /* Adaptation flags: PCR present */
558
559
    /* PCR coded into 6 bytes */
560
    *q++ = pcr >> 25;
561
    *q++ = pcr >> 17;
562
    *q++ = pcr >> 9;
563
    *q++ = pcr >> 1;
564
    *q++ = (pcr & 1) << 7;
565
    *q++ = 0;
566
567
    /* stuffing bytes */
568
    memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
569
    put_buffer(s->pb, buf, TS_PACKET_SIZE);
570
    ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
571
}
572
573 700b9711 Måns Rullgård
static void write_pts(uint8_t *q, int fourbits, int64_t pts)
574
{
575
    int val;
576
577
    val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
578
    *q++ = val;
579
    val = (((pts >> 15) & 0x7fff) << 1) | 1;
580
    *q++ = val >> 8;
581
    *q++ = val;
582
    val = (((pts) & 0x7fff) << 1) | 1;
583
    *q++ = val >> 8;
584
    *q++ = val;
585
}
586
587 c054f372 Mike Scheutzow
/* Add a pes header to the front of payload, and segment into an integer number of
588
 * ts packets. The final ts packet is padded using an over-sized adaptation header
589
 * to exactly fill the last ts packet.
590
 * NOTE: 'payload' contains a complete PES payload.
591
 */
592 69ef9450 Fabrice Bellard
static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
593
                             const uint8_t *payload, int payload_size,
594 700b9711 Måns Rullgård
                             int64_t pts, int64_t dts)
595 5dbafeb7 Fabrice Bellard
{
596
    MpegTSWriteStream *ts_st = st->priv_data;
597 b5931348 Baptiste Coudurier
    MpegTSWrite *ts = s->priv_data;
598 69ef9450 Fabrice Bellard
    uint8_t buf[TS_PACKET_SIZE];
599 5dbafeb7 Fabrice Bellard
    uint8_t *q;
600 700b9711 Måns Rullgård
    int val, is_start, len, header_len, write_pcr, private_code, flags;
601 8b475508 Fabrice Bellard
    int afc_len, stuffing_len;
602
    int64_t pcr = -1; /* avoid warning */
603 4df3bbbc Mike Scheutzow
    int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
604 5dbafeb7 Fabrice Bellard
605 69ef9450 Fabrice Bellard
    is_start = 1;
606
    while (payload_size > 0) {
607
        retransmit_si_info(s);
608 5dbafeb7 Fabrice Bellard
609 8b475508 Fabrice Bellard
        write_pcr = 0;
610
        if (ts_st->pid == ts_st->service->pcr_pid) {
611 7082ea56 Baptiste Coudurier
            if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
612
                ts_st->service->pcr_packet_count++;
613 115329f1 Diego Biurrun
            if (ts_st->service->pcr_packet_count >=
614 1aae3489 Niobos
                ts_st->service->pcr_packet_period) {
615 8b475508 Fabrice Bellard
                ts_st->service->pcr_packet_count = 0;
616
                write_pcr = 1;
617
            }
618
        }
619
620 7082ea56 Baptiste Coudurier
        if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
621
            (dts - (int64_t)ts->cur_pcr) > delay) {
622 4df3bbbc Mike Scheutzow
            /* pcr insert gets priority over null packet insert */
623
            if (write_pcr)
624
                mpegts_insert_pcr_only(s, st);
625
            else
626
                mpegts_insert_null_packet(s);
627
            continue; /* recalculate write_pcr and possibly retransmit si_info */
628
        }
629
630 69ef9450 Fabrice Bellard
        /* prepare packet header */
631
        q = buf;
632
        *q++ = 0x47;
633
        val = (ts_st->pid >> 8);
634
        if (is_start)
635
            val |= 0x40;
636
        *q++ = val;
637
        *q++ = ts_st->pid;
638
        ts_st->cc = (ts_st->cc + 1) & 0xf;
639 8d819221 Yann Coupin
        *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
640 8b475508 Fabrice Bellard
        if (write_pcr) {
641 b5931348 Baptiste Coudurier
            // add 11, pcr references the last byte of program clock reference base
642 7082ea56 Baptiste Coudurier
            if (ts->mux_rate > 1)
643
                pcr = ts->cur_pcr + (4+7)*8*90000LL / ts->mux_rate;
644
            else
645
                pcr = dts - delay;
646 811a0aa7 Baptiste Coudurier
            if (dts != AV_NOPTS_VALUE && dts < pcr)
647
                av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
648 8b475508 Fabrice Bellard
            *q++ = 7; /* AFC length */
649
            *q++ = 0x10; /* flags: PCR present */
650
            *q++ = pcr >> 25;
651
            *q++ = pcr >> 17;
652
            *q++ = pcr >> 9;
653
            *q++ = pcr >> 1;
654
            *q++ = (pcr & 1) << 7;
655
            *q++ = 0;
656
        }
657 69ef9450 Fabrice Bellard
        if (is_start) {
658 f4bba201 Anuradha Suraparaju
            int pes_extension = 0;
659 69ef9450 Fabrice Bellard
            /* write PES header */
660
            *q++ = 0x00;
661
            *q++ = 0x00;
662
            *q++ = 0x01;
663 8b475508 Fabrice Bellard
            private_code = 0;
664 01f4895c Michael Niedermayer
            if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
665 f4bba201 Anuradha Suraparaju
                if (st->codec->codec_id == CODEC_ID_DIRAC) {
666
                    *q++ = 0xfd;
667
                } else
668
                    *q++ = 0xe0;
669 01f4895c Michael Niedermayer
            } else if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
670
                       (st->codec->codec_id == CODEC_ID_MP2 ||
671
                        st->codec->codec_id == CODEC_ID_MP3)) {
672 115329f1 Diego Biurrun
                *q++ = 0xc0;
673 8b475508 Fabrice Bellard
            } else {
674
                *q++ = 0xbd;
675 01f4895c Michael Niedermayer
                if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
676 8b475508 Fabrice Bellard
                    private_code = 0x20;
677
                }
678
            }
679 700b9711 Måns Rullgård
            header_len = 0;
680
            flags = 0;
681
            if (pts != AV_NOPTS_VALUE) {
682
                header_len += 5;
683
                flags |= 0x80;
684
            }
685 8f14cdee Baptiste Coudurier
            if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
686 700b9711 Måns Rullgård
                header_len += 5;
687
                flags |= 0x40;
688
            }
689 f4bba201 Anuradha Suraparaju
            if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
690
                st->codec->codec_id == CODEC_ID_DIRAC) {
691
                /* set PES_extension_flag */
692
                pes_extension = 1;
693
                flags |= 0x01;
694
695
                /*
696
                * One byte for PES2 extension flag +
697
                * one byte for extension length +
698
                * one byte for extension id
699
                */
700
                header_len += 3;
701
            }
702 700b9711 Måns Rullgård
            len = payload_size + header_len + 3;
703 8b475508 Fabrice Bellard
            if (private_code != 0)
704 700b9711 Måns Rullgård
                len++;
705 e4358e70 Baptiste Coudurier
            if (len > 0xffff)
706
                len = 0;
707 69ef9450 Fabrice Bellard
            *q++ = len >> 8;
708
            *q++ = len;
709 8b475508 Fabrice Bellard
            val = 0x80;
710
            /* data alignment indicator is required for subtitle data */
711 01f4895c Michael Niedermayer
            if (st->codec->codec_type == CODEC_TYPE_SUBTITLE)
712 8b475508 Fabrice Bellard
                val |= 0x04;
713
            *q++ = val;
714 700b9711 Måns Rullgård
            *q++ = flags;
715
            *q++ = header_len;
716 69ef9450 Fabrice Bellard
            if (pts != AV_NOPTS_VALUE) {
717 700b9711 Måns Rullgård
                write_pts(q, flags >> 6, pts);
718
                q += 5;
719
            }
720 8f14cdee Baptiste Coudurier
            if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
721 700b9711 Måns Rullgård
                write_pts(q, 1, dts);
722
                q += 5;
723 5dbafeb7 Fabrice Bellard
            }
724 f4bba201 Anuradha Suraparaju
            if (pes_extension && st->codec->codec_id == CODEC_ID_DIRAC) {
725
                flags = 0x01;  /* set PES_extension_flag_2 */
726
                *q++ = flags;
727
                *q++ = 0x80 | 0x01;  /* marker bit + extension length */
728
                /*
729
                * Set the stream id extension flag bit to 0 and
730
                * write the extended stream id
731
                */
732
                *q++ = 0x00 | 0x60;
733
            }
734 8b475508 Fabrice Bellard
            if (private_code != 0)
735
                *q++ = private_code;
736 69ef9450 Fabrice Bellard
            is_start = 0;
737 5dbafeb7 Fabrice Bellard
        }
738 8b475508 Fabrice Bellard
        /* header size */
739
        header_len = q - buf;
740
        /* data len */
741
        len = TS_PACKET_SIZE - header_len;
742 69ef9450 Fabrice Bellard
        if (len > payload_size)
743
            len = payload_size;
744 8b475508 Fabrice Bellard
        stuffing_len = TS_PACKET_SIZE - header_len - len;
745
        if (stuffing_len > 0) {
746
            /* add stuffing with AFC */
747
            if (buf[3] & 0x20) {
748
                /* stuffing already present: increase its size */
749
                afc_len = buf[4] + 1;
750
                memmove(buf + 4 + afc_len + stuffing_len,
751 115329f1 Diego Biurrun
                        buf + 4 + afc_len,
752 8b475508 Fabrice Bellard
                        header_len - (4 + afc_len));
753
                buf[4] += stuffing_len;
754
                memset(buf + 4 + afc_len, 0xff, stuffing_len);
755
            } else {
756
                /* add stuffing */
757
                memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
758
                buf[3] |= 0x20;
759
                buf[4] = stuffing_len - 1;
760
                if (stuffing_len >= 2) {
761
                    buf[5] = 0x00;
762
                    memset(buf + 6, 0xff, stuffing_len - 2);
763
                }
764
            }
765
        }
766
        memcpy(buf + TS_PACKET_SIZE - len, payload, len);
767 69ef9450 Fabrice Bellard
        payload += len;
768
        payload_size -= len;
769 899681cd Björn Axelsson
        put_buffer(s->pb, buf, TS_PACKET_SIZE);
770 b5931348 Baptiste Coudurier
        ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
771 5dbafeb7 Fabrice Bellard
    }
772 899681cd Björn Axelsson
    put_flush_packet(s->pb);
773 69ef9450 Fabrice Bellard
}
774
775 e928649b Michael Niedermayer
static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
776 69ef9450 Fabrice Bellard
{
777 e928649b Michael Niedermayer
    AVStream *st = s->streams[pkt->stream_index];
778 01d6bd52 Baptiste Coudurier
    int size = pkt->size;
779 e928649b Michael Niedermayer
    uint8_t *buf= pkt->data;
780 a57fb91c Baptiste Coudurier
    uint8_t *data= NULL;
781 69ef9450 Fabrice Bellard
    MpegTSWriteStream *ts_st = st->priv_data;
782 7082ea56 Baptiste Coudurier
    const uint64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE)*2;
783 f3ba7c54 Baptiste Coudurier
    int64_t dts = AV_NOPTS_VALUE, pts = AV_NOPTS_VALUE;
784
785
    if (pkt->pts != AV_NOPTS_VALUE)
786
        pts = pkt->pts + delay;
787
    if (pkt->dts != AV_NOPTS_VALUE)
788
        dts = pkt->dts + delay;
789 69ef9450 Fabrice Bellard
790 b69017af Baptiste Coudurier
    if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
791
        av_log(s, AV_LOG_ERROR, "first pts value must set\n");
792
        return -1;
793
    }
794
    ts_st->first_pts_check = 0;
795
796 e4358e70 Baptiste Coudurier
    if (st->codec->codec_id == CODEC_ID_H264) {
797 e17d77bb Baptiste Coudurier
        const uint8_t *p = buf, *buf_end = p+size;
798
        uint32_t state = -1;
799
800 a57fb91c Baptiste Coudurier
        if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
801 e17d77bb Baptiste Coudurier
            av_log(s, AV_LOG_ERROR, "h264 bitstream malformated, "
802
                   "no startcode found, use -vbsf h264_mp4toannexb\n");
803 a57fb91c Baptiste Coudurier
            return -1;
804
        }
805 e17d77bb Baptiste Coudurier
806
        do {
807
            p = ff_find_start_code(p, buf_end, &state);
808
            //av_log(s, AV_LOG_INFO, "nal %d\n", state & 0x1f);
809
        } while (p < buf_end && (state & 0x1f) != 9 &&
810
                 (state & 0x1f) != 5 && (state & 0x1f) != 1);
811
812
        if ((state & 0x1f) != 9) { // AUD NAL
813 a57fb91c Baptiste Coudurier
            data = av_malloc(pkt->size+6);
814
            if (!data)
815
                return -1;
816
            memcpy(data+6, pkt->data, pkt->size);
817
            AV_WB32(data, 0x00000001);
818
            data[4] = 0x09;
819
            data[5] = 0xe0; // any slice type
820
            buf  = data;
821
            size = pkt->size+6;
822
        }
823 8fdd542c Baptiste Coudurier
    } else if (st->codec->codec_id == CODEC_ID_AAC) {
824
        if (pkt->size < 2)
825
            return -1;
826
        if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
827
            ADTSContext *adts = ts_st->adts;
828
            int new_size;
829
            if (!adts) {
830
                av_log(s, AV_LOG_ERROR, "aac bitstream not in adts format "
831
                       "and extradata missing\n");
832
                return -1;
833
            }
834
            new_size = ADTS_HEADER_SIZE+adts->pce_size+pkt->size;
835
            if ((unsigned)new_size >= INT_MAX)
836
                return -1;
837
            data = av_malloc(new_size);
838
            if (!data)
839
                return AVERROR_NOMEM;
840
            ff_adts_write_frame_header(adts, data, pkt->size, adts->pce_size);
841
            if (adts->pce_size) {
842
                memcpy(data+ADTS_HEADER_SIZE, adts->pce_data, adts->pce_size);
843
                adts->pce_size = 0;
844
            }
845
            memcpy(data+ADTS_HEADER_SIZE+adts->pce_size, pkt->data, pkt->size);
846
            buf = data;
847
            size = new_size;
848
        }
849 de34dc39 Baptiste Coudurier
    }
850
851 807e4e81 Baptiste Coudurier
    if (st->codec->codec_type != CODEC_TYPE_AUDIO) {
852 e4358e70 Baptiste Coudurier
        // for video and subtitle, write a single pes packet
853
        mpegts_write_pes(s, st, buf, size, pts, dts);
854 24ac5052 Baptiste Coudurier
        av_free(data);
855 e4358e70 Baptiste Coudurier
        return 0;
856
    }
857
858 01d6bd52 Baptiste Coudurier
    if (ts_st->payload_index + size > DEFAULT_PES_PAYLOAD_SIZE) {
859
        mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
860
                         ts_st->payload_pts, ts_st->payload_dts);
861
        ts_st->payload_index = 0;
862 84df78b4 Baptiste Coudurier
    }
863
864 01d6bd52 Baptiste Coudurier
    if (!ts_st->payload_index) {
865
        ts_st->payload_pts = pts;
866
        ts_st->payload_dts = dts;
867 69ef9450 Fabrice Bellard
    }
868 a57fb91c Baptiste Coudurier
869 01d6bd52 Baptiste Coudurier
    memcpy(ts_st->payload + ts_st->payload_index, buf, size);
870
    ts_st->payload_index += size;
871
872 83c2bc7a Baptiste Coudurier
    av_free(data);
873
874 5dbafeb7 Fabrice Bellard
    return 0;
875
}
876
877
static int mpegts_write_end(AVFormatContext *s)
878
{
879
    MpegTSWrite *ts = s->priv_data;
880
    MpegTSWriteStream *ts_st;
881
    MpegTSService *service;
882
    AVStream *st;
883
    int i;
884
885
    /* flush current packets */
886
    for(i = 0; i < s->nb_streams; i++) {
887
        st = s->streams[i];
888
        ts_st = st->priv_data;
889 69ef9450 Fabrice Bellard
        if (ts_st->payload_index > 0) {
890
            mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
891 700b9711 Måns Rullgård
                             ts_st->payload_pts, ts_st->payload_dts);
892 5dbafeb7 Fabrice Bellard
        }
893 83c2bc7a Baptiste Coudurier
        av_freep(&ts_st->adts);
894 5dbafeb7 Fabrice Bellard
    }
895 899681cd Björn Axelsson
    put_flush_packet(s->pb);
896 115329f1 Diego Biurrun
897 5dbafeb7 Fabrice Bellard
    for(i = 0; i < ts->nb_services; i++) {
898
        service = ts->services[i];
899
        av_freep(&service->provider_name);
900
        av_freep(&service->name);
901
        av_free(service);
902
    }
903
    av_free(ts->services);
904
905
    return 0;
906
}
907
908 d2a067d1 Måns Rullgård
AVOutputFormat mpegts_muxer = {
909 5dbafeb7 Fabrice Bellard
    "mpegts",
910 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
911 5dbafeb7 Fabrice Bellard
    "video/x-mpegts",
912 d3ad044a Baptiste Coudurier
    "ts,m2t",
913 5dbafeb7 Fabrice Bellard
    sizeof(MpegTSWrite),
914
    CODEC_ID_MP2,
915 69ef9450 Fabrice Bellard
    CODEC_ID_MPEG2VIDEO,
916 5dbafeb7 Fabrice Bellard
    mpegts_write_header,
917
    mpegts_write_packet,
918
    mpegts_write_end,
919
};