2 * MPEG2 transport stream (aka DVB) muxer
3 * Copyright (c) 2003 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 /* write DVB SI sections */
27 /*********************************************/
28 /* mpegts section writer */
30 typedef struct MpegTSSection
{
33 void (*write_packet
)(struct MpegTSSection
*s
, const uint8_t *packet
);
37 /* NOTE: 4 bytes must be left at the end for the crc32 */
38 static void mpegts_write_section(MpegTSSection
*s
, uint8_t *buf
, int len
)
41 unsigned char packet
[TS_PACKET_SIZE
];
42 const unsigned char *buf_ptr
;
44 int first
, b
, len1
, left
;
46 crc
= bswap_32(av_crc(av_crc04C11DB7
, -1, buf
, len
- 4));
47 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;
52 /* send each packet */
55 first
= (buf
== buf_ptr
);
63 s
->cc
= (s
->cc
+ 1) & 0xf;
66 *q
++ = 0; /* 0 offset */
67 len1
= TS_PACKET_SIZE
- (q
- packet
);
70 memcpy(q
, buf_ptr
, len1
);
72 /* add known padding data */
73 left
= TS_PACKET_SIZE
- (q
- packet
);
75 memset(q
, 0xff, left
);
77 s
->write_packet(s
, packet
);
84 static inline void put16(uint8_t **q_ptr
, int val
)
93 static int mpegts_write_section1(MpegTSSection
*s
, int tid
, int id
,
94 int version
, int sec_num
, int last_sec_num
,
95 uint8_t *buf
, int len
)
97 uint8_t section
[1024], *q
;
100 tot_len
= 3 + 5 + len
+ 4;
101 /* check if not too big */
107 put16(&q
, 0xb000 | (len
+ 5 + 4)); /* 5 byte header + 4 byte CRC */
109 *q
++ = 0xc1 | (version
<< 1); /* current_next_indicator = 1 */
114 mpegts_write_section(s
, section
, tot_len
);
118 /*********************************************/
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"
126 /* default network id, transport stream and service identifiers */
127 #define DEFAULT_ONID 0x0001
128 #define DEFAULT_TSID 0x0001
129 #define DEFAULT_SID 0x0001
131 /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
132 #define DEFAULT_PES_HEADER_FREQ 16
133 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
135 /* we retransmit the SI info at this rate */
136 #define SDT_RETRANS_TIME 500
137 #define PAT_RETRANS_TIME 100
138 #define PCR_RETRANS_TIME 20
140 typedef struct MpegTSWriteStream
{
141 struct MpegTSService
*service
;
142 int pid
; /* stream associated pid */
147 uint8_t payload
[DEFAULT_PES_PAYLOAD_SIZE
];
150 typedef struct MpegTSService
{
151 MpegTSSection pmt
; /* MPEG2 pmt table context */
152 int sid
; /* service ID */
156 int pcr_packet_count
;
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
;
166 int pat_packet_count
;
173 static void mpegts_write_pat(AVFormatContext
*s
)
175 MpegTSWrite
*ts
= s
->priv_data
;
176 MpegTSService
*service
;
177 uint8_t data
[1012], *q
;
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
);
186 mpegts_write_section1(&ts
->pat
, PAT_TID
, ts
->tsid
, 0, 0, 0,
190 static void mpegts_write_pmt(AVFormatContext
*s
, MpegTSService
*service
)
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
;
197 put16(&q
, 0xe000 | service
->pcr_pid
);
199 program_info_length_ptr
= q
;
200 q
+= 2; /* patched after */
202 /* put program info here */
204 val
= 0xf000 | (q
- program_info_length_ptr
- 2);
205 program_info_length_ptr
[0] = val
>> 8;
206 program_info_length_ptr
[1] = val
;
208 for(i
= 0; i
< s
->nb_streams
; i
++) {
209 AVStream
*st
= s
->streams
[i
];
210 MpegTSWriteStream
*ts_st
= st
->priv_data
;
211 switch(st
->codec
->codec_id
) {
212 case CODEC_ID_MPEG1VIDEO
:
213 case CODEC_ID_MPEG2VIDEO
:
214 stream_type
= STREAM_TYPE_VIDEO_MPEG2
;
217 stream_type
= STREAM_TYPE_VIDEO_MPEG4
;
220 stream_type
= STREAM_TYPE_VIDEO_H264
;
224 stream_type
= STREAM_TYPE_AUDIO_MPEG1
;
227 stream_type
= STREAM_TYPE_AUDIO_AAC
;
230 stream_type
= STREAM_TYPE_AUDIO_AC3
;
233 stream_type
= STREAM_TYPE_PRIVATE_DATA
;
237 put16(&q
, 0xe000 | ts_st
->pid
);
239 q
+= 2; /* patched after */
241 /* write optional descriptors here */
242 switch(st
->codec
->codec_type
) {
243 case CODEC_TYPE_AUDIO
:
244 if (strlen(st
->language
) == 3) {
245 *q
++ = 0x0a; /* ISO 639 language descriptor */
247 *q
++ = st
->language
[0];
248 *q
++ = st
->language
[1];
249 *q
++ = st
->language
[2];
250 *q
++ = 0; /* undefined type */
253 case CODEC_TYPE_SUBTITLE
:
255 const char *language
;
256 language
= st
->language
;
257 if (strlen(language
) != 3)
264 *q
++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
265 put16(&q
, 1); /* page id */
266 put16(&q
, 1); /* ancillary page id */
271 val
= 0xf000 | (q
- desc_length_ptr
- 2);
272 desc_length_ptr
[0] = val
>> 8;
273 desc_length_ptr
[1] = val
;
275 mpegts_write_section1(&service
->pmt
, PMT_TID
, service
->sid
, 0, 0, 0,
279 /* NOTE: str == NULL is accepted for an empty string */
280 static void putstr8(uint8_t **q_ptr
, const char *str
)
296 static void mpegts_write_sdt(AVFormatContext
*s
)
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
;
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
;
312 running_status
= 4; /* running */
315 /* write only one descriptor for the service name and provider */
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;
324 /* fill descriptor length */
325 val
= (running_status
<< 13) | (free_ca_mode
<< 12) |
326 (q
- desc_list_len_ptr
- 2);
327 desc_list_len_ptr
[0] = val
>> 8;
328 desc_list_len_ptr
[1] = val
;
330 mpegts_write_section1(&ts
->sdt
, SDT_TID
, ts
->tsid
, 0, 0, 0,
334 static MpegTSService
*mpegts_add_service(MpegTSWrite
*ts
,
336 const char *provider_name
,
339 MpegTSService
*service
;
341 service
= av_mallocz(sizeof(MpegTSService
));
344 service
->pmt
.pid
= DEFAULT_PMT_START_PID
+ ts
->nb_services
- 1;
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
);
353 static void section_write_packet(MpegTSSection
*s
, const uint8_t *packet
)
355 AVFormatContext
*ctx
= s
->opaque
;
356 put_buffer(ctx
->pb
, packet
, TS_PACKET_SIZE
);
359 static int mpegts_write_header(AVFormatContext
*s
)
361 MpegTSWrite
*ts
= s
->priv_data
;
362 MpegTSWriteStream
*ts_st
;
363 MpegTSService
*service
;
365 int i
, total_bit_rate
;
366 const char *service_name
;
368 ts
->tsid
= DEFAULT_TSID
;
369 ts
->onid
= DEFAULT_ONID
;
370 /* allocate a single DVB service */
371 service_name
= s
->title
;
372 if (service_name
[0] == '\0')
373 service_name
= DEFAULT_SERVICE_NAME
;
374 service
= mpegts_add_service(ts
, DEFAULT_SID
,
375 DEFAULT_PROVIDER_NAME
, service_name
);
376 service
->pmt
.write_packet
= section_write_packet
;
377 service
->pmt
.opaque
= s
;
379 ts
->pat
.pid
= PAT_PID
;
381 ts
->pat
.write_packet
= section_write_packet
;
384 ts
->sdt
.pid
= SDT_PID
;
386 ts
->sdt
.write_packet
= section_write_packet
;
389 /* assign pids to each stream */
391 for(i
= 0;i
< s
->nb_streams
; i
++) {
393 ts_st
= av_mallocz(sizeof(MpegTSWriteStream
));
396 st
->priv_data
= ts_st
;
397 ts_st
->service
= service
;
398 ts_st
->pid
= DEFAULT_START_PID
+ i
;
399 ts_st
->payload_pts
= AV_NOPTS_VALUE
;
400 ts_st
->payload_dts
= AV_NOPTS_VALUE
;
401 /* update PCR pid by using the first video stream */
402 if (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
&&
403 service
->pcr_pid
== 0x1fff)
404 service
->pcr_pid
= ts_st
->pid
;
405 total_bit_rate
+= st
->codec
->bit_rate
;
408 /* 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
;
414 if (total_bit_rate
<= 8 * 1024)
415 total_bit_rate
= 8 * 1024;
416 service
->pcr_packet_freq
= (total_bit_rate
* PCR_RETRANS_TIME
) /
417 (TS_PACKET_SIZE
* 8 * 1000);
418 ts
->sdt_packet_freq
= (total_bit_rate
* SDT_RETRANS_TIME
) /
419 (TS_PACKET_SIZE
* 8 * 1000);
420 ts
->pat_packet_freq
= (total_bit_rate
* PAT_RETRANS_TIME
) /
421 (TS_PACKET_SIZE
* 8 * 1000);
424 total_bit_rate
, ts
->sdt_packet_freq
, ts
->pat_packet_freq
);
427 /* write info at the start of the file, so that it will be fast to
431 for(i
= 0; i
< ts
->nb_services
; i
++) {
432 mpegts_write_pmt(s
, ts
->services
[i
]);
434 put_flush_packet(s
->pb
);
439 for(i
= 0;i
< s
->nb_streams
; i
++) {
441 av_free(st
->priv_data
);
446 /* send SDT, PAT and PMT tables regulary */
447 static void retransmit_si_info(AVFormatContext
*s
)
449 MpegTSWrite
*ts
= s
->priv_data
;
452 if (++ts
->sdt_packet_count
== ts
->sdt_packet_freq
) {
453 ts
->sdt_packet_count
= 0;
456 if (++ts
->pat_packet_count
== ts
->pat_packet_freq
) {
457 ts
->pat_packet_count
= 0;
459 for(i
= 0; i
< ts
->nb_services
; i
++) {
460 mpegts_write_pmt(s
, ts
->services
[i
]);
465 static void write_pts(uint8_t *q
, int fourbits
, int64_t pts
)
469 val
= fourbits
<< 4 | (((pts
>> 30) & 0x07) << 1) | 1;
471 val
= (((pts
>> 15) & 0x7fff) << 1) | 1;
474 val
= (((pts
) & 0x7fff) << 1) | 1;
479 /* 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 int64_t pts
, int64_t dts
)
484 MpegTSWriteStream
*ts_st
= st
->priv_data
;
485 uint8_t buf
[TS_PACKET_SIZE
];
487 int val
, is_start
, len
, header_len
, write_pcr
, private_code
, flags
;
488 int afc_len
, stuffing_len
;
489 int64_t pcr
= -1; /* avoid warning */
492 while (payload_size
> 0) {
493 retransmit_si_info(s
);
496 if (ts_st
->pid
== ts_st
->service
->pcr_pid
) {
497 ts_st
->service
->pcr_packet_count
++;
498 if (ts_st
->service
->pcr_packet_count
>=
499 ts_st
->service
->pcr_packet_freq
) {
500 ts_st
->service
->pcr_packet_count
= 0;
502 /* XXX: this is incorrect, but at least we have a PCR
508 /* prepare packet header */
511 val
= (ts_st
->pid
>> 8);
516 *q
++ = 0x10 | ts_st
->cc
| (write_pcr
? 0x20 : 0);
517 ts_st
->cc
= (ts_st
->cc
+ 1) & 0xf;
519 *q
++ = 7; /* AFC length */
520 *q
++ = 0x10; /* flags: PCR present */
525 *q
++ = (pcr
& 1) << 7;
529 /* write PES header */
534 if (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
) {
536 } 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
)) {
542 if (st
->codec
->codec_type
== CODEC_TYPE_SUBTITLE
) {
548 if (pts
!= AV_NOPTS_VALUE
) {
552 if (dts
!= AV_NOPTS_VALUE
) {
556 len
= payload_size
+ header_len
+ 3;
557 if (private_code
!= 0)
562 /* data alignment indicator is required for subtitle data */
563 if (st
->codec
->codec_type
== CODEC_TYPE_SUBTITLE
)
568 if (pts
!= AV_NOPTS_VALUE
) {
569 write_pts(q
, flags
>> 6, pts
);
572 if (dts
!= AV_NOPTS_VALUE
) {
573 write_pts(q
, 1, dts
);
576 if (private_code
!= 0)
581 header_len
= q
- buf
;
583 len
= TS_PACKET_SIZE
- header_len
;
584 if (len
> payload_size
)
586 stuffing_len
= TS_PACKET_SIZE
- header_len
- len
;
587 if (stuffing_len
> 0) {
588 /* add stuffing with AFC */
590 /* stuffing already present: increase its size */
591 afc_len
= buf
[4] + 1;
592 memmove(buf
+ 4 + afc_len
+ stuffing_len
,
594 header_len
- (4 + afc_len
));
595 buf
[4] += stuffing_len
;
596 memset(buf
+ 4 + afc_len
, 0xff, stuffing_len
);
599 memmove(buf
+ 4 + stuffing_len
, buf
+ 4, header_len
- 4);
601 buf
[4] = stuffing_len
- 1;
602 if (stuffing_len
>= 2) {
604 memset(buf
+ 6, 0xff, stuffing_len
- 2);
608 memcpy(buf
+ TS_PACKET_SIZE
- len
, payload
, len
);
611 put_buffer(s
->pb
, buf
, TS_PACKET_SIZE
);
613 put_flush_packet(s
->pb
);
616 static int mpegts_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
618 AVStream
*st
= s
->streams
[pkt
->stream_index
];
620 uint8_t *buf
= pkt
->data
;
621 MpegTSWriteStream
*ts_st
= st
->priv_data
;
622 int len
, max_payload_size
;
624 if (st
->codec
->codec_type
== CODEC_TYPE_SUBTITLE
) {
625 /* for subtitle, a single PES packet must be generated */
626 mpegts_write_pes(s
, st
, buf
, size
, pkt
->pts
, AV_NOPTS_VALUE
);
630 max_payload_size
= DEFAULT_PES_PAYLOAD_SIZE
;
632 len
= max_payload_size
- ts_st
->payload_index
;
635 memcpy(ts_st
->payload
+ ts_st
->payload_index
, buf
, len
);
638 ts_st
->payload_index
+= len
;
639 if (ts_st
->payload_pts
== AV_NOPTS_VALUE
)
640 ts_st
->payload_pts
= pkt
->pts
;
641 if (ts_st
->payload_dts
== AV_NOPTS_VALUE
)
642 ts_st
->payload_dts
= pkt
->dts
;
643 if (ts_st
->payload_index
>= max_payload_size
) {
644 mpegts_write_pes(s
, st
, ts_st
->payload
, ts_st
->payload_index
,
645 ts_st
->payload_pts
, ts_st
->payload_dts
);
646 ts_st
->payload_pts
= AV_NOPTS_VALUE
;
647 ts_st
->payload_dts
= AV_NOPTS_VALUE
;
648 ts_st
->payload_index
= 0;
654 static int mpegts_write_end(AVFormatContext
*s
)
656 MpegTSWrite
*ts
= s
->priv_data
;
657 MpegTSWriteStream
*ts_st
;
658 MpegTSService
*service
;
662 /* flush current packets */
663 for(i
= 0; i
< s
->nb_streams
; i
++) {
665 ts_st
= st
->priv_data
;
666 if (ts_st
->payload_index
> 0) {
667 mpegts_write_pes(s
, st
, ts_st
->payload
, ts_st
->payload_index
,
668 ts_st
->payload_pts
, ts_st
->payload_dts
);
671 put_flush_packet(s
->pb
);
673 for(i
= 0; i
< ts
->nb_services
; i
++) {
674 service
= ts
->services
[i
];
675 av_freep(&service
->provider_name
);
676 av_freep(&service
->name
);
679 av_free(ts
->services
);
684 AVOutputFormat mpegts_muxer
= {
686 "MPEG2 transport stream format",