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
22 #include "libavutil/crc.h"
26 /* write DVB SI sections */
28 /*********************************************/
29 /* mpegts section writer */
31 typedef struct MpegTSSection
{
34 void (*write_packet
)(struct MpegTSSection
*s
, const uint8_t *packet
);
38 /* NOTE: 4 bytes must be left at the end for the crc32 */
39 static void mpegts_write_section(MpegTSSection
*s
, uint8_t *buf
, int len
)
42 unsigned char packet
[TS_PACKET_SIZE
];
43 const unsigned char *buf_ptr
;
45 int first
, b
, len1
, left
;
47 crc
= bswap_32(av_crc(av_crc_get_table(AV_CRC_32_IEEE
), -1, buf
, len
- 4));
48 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;
53 /* send each packet */
56 first
= (buf
== buf_ptr
);
65 s
->cc
= (s
->cc
+ 1) & 0xf;
67 *q
++ = 0; /* 0 offset */
68 len1
= TS_PACKET_SIZE
- (q
- packet
);
71 memcpy(q
, buf_ptr
, len1
);
73 /* add known padding data */
74 left
= TS_PACKET_SIZE
- (q
- packet
);
76 memset(q
, 0xff, left
);
78 s
->write_packet(s
, packet
);
85 static inline void put16(uint8_t **q_ptr
, int val
)
94 static int mpegts_write_section1(MpegTSSection
*s
, int tid
, int id
,
95 int version
, int sec_num
, int last_sec_num
,
96 uint8_t *buf
, int len
)
98 uint8_t section
[1024], *q
;
101 tot_len
= 3 + 5 + len
+ 4;
102 /* check if not too big */
108 put16(&q
, 0xb000 | (len
+ 5 + 4)); /* 5 byte header + 4 byte CRC */
110 *q
++ = 0xc1 | (version
<< 1); /* current_next_indicator = 1 */
115 mpegts_write_section(s
, section
, tot_len
);
119 /*********************************************/
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"
127 /* default network id, transport stream and service identifiers */
128 #define DEFAULT_ONID 0x0001
129 #define DEFAULT_TSID 0x0001
130 #define DEFAULT_SID 0x0001
132 /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
133 #define DEFAULT_PES_HEADER_FREQ 16
134 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
136 /* we retransmit the SI info at this rate */
137 #define SDT_RETRANS_TIME 500
138 #define PAT_RETRANS_TIME 100
139 #define PCR_RETRANS_TIME 20
141 typedef struct MpegTSWriteStream
{
142 struct MpegTSService
*service
;
143 int pid
; /* stream associated pid */
148 uint8_t payload
[DEFAULT_PES_PAYLOAD_SIZE
];
151 typedef struct MpegTSService
{
152 MpegTSSection pmt
; /* MPEG2 pmt table context */
153 int sid
; /* service ID */
157 int pcr_packet_count
;
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
;
167 int pat_packet_count
;
174 static void mpegts_write_pat(AVFormatContext
*s
)
176 MpegTSWrite
*ts
= s
->priv_data
;
177 MpegTSService
*service
;
178 uint8_t data
[1012], *q
;
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
);
187 mpegts_write_section1(&ts
->pat
, PAT_TID
, ts
->tsid
, 0, 0, 0,
191 static void mpegts_write_pmt(AVFormatContext
*s
, MpegTSService
*service
)
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
;
198 put16(&q
, 0xe000 | service
->pcr_pid
);
200 program_info_length_ptr
= q
;
201 q
+= 2; /* patched after */
203 /* put program info here */
205 val
= 0xf000 | (q
- program_info_length_ptr
- 2);
206 program_info_length_ptr
[0] = val
>> 8;
207 program_info_length_ptr
[1] = val
;
209 for(i
= 0; i
< s
->nb_streams
; i
++) {
210 AVStream
*st
= s
->streams
[i
];
211 MpegTSWriteStream
*ts_st
= st
->priv_data
;
212 switch(st
->codec
->codec_id
) {
213 case CODEC_ID_MPEG1VIDEO
:
214 case CODEC_ID_MPEG2VIDEO
:
215 stream_type
= STREAM_TYPE_VIDEO_MPEG2
;
218 stream_type
= STREAM_TYPE_VIDEO_MPEG4
;
221 stream_type
= STREAM_TYPE_VIDEO_H264
;
224 stream_type
= STREAM_TYPE_VIDEO_DIRAC
;
228 stream_type
= STREAM_TYPE_AUDIO_MPEG1
;
231 stream_type
= STREAM_TYPE_AUDIO_AAC
;
234 stream_type
= STREAM_TYPE_AUDIO_AC3
;
237 stream_type
= STREAM_TYPE_PRIVATE_DATA
;
241 put16(&q
, 0xe000 | ts_st
->pid
);
243 q
+= 2; /* patched after */
245 /* write optional descriptors here */
246 switch(st
->codec
->codec_type
) {
247 case CODEC_TYPE_AUDIO
:
248 if (strlen(st
->language
) == 3) {
249 *q
++ = 0x0a; /* ISO 639 language descriptor */
251 *q
++ = st
->language
[0];
252 *q
++ = st
->language
[1];
253 *q
++ = st
->language
[2];
254 *q
++ = 0; /* undefined type */
257 case CODEC_TYPE_SUBTITLE
:
259 const char *language
;
260 language
= st
->language
;
261 if (strlen(language
) != 3)
268 *q
++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
269 put16(&q
, 1); /* page id */
270 put16(&q
, 1); /* ancillary page id */
273 case CODEC_TYPE_VIDEO
:
274 if (stream_type
== STREAM_TYPE_VIDEO_DIRAC
) {
275 *q
++ = 0x05; /*MPEG-2 registration descriptor*/
285 val
= 0xf000 | (q
- desc_length_ptr
- 2);
286 desc_length_ptr
[0] = val
>> 8;
287 desc_length_ptr
[1] = val
;
289 mpegts_write_section1(&service
->pmt
, PMT_TID
, service
->sid
, 0, 0, 0,
293 /* NOTE: str == NULL is accepted for an empty string */
294 static void putstr8(uint8_t **q_ptr
, const char *str
)
310 static void mpegts_write_sdt(AVFormatContext
*s
)
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
;
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
;
326 running_status
= 4; /* running */
329 /* write only one descriptor for the service name and provider */
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;
338 /* fill descriptor length */
339 val
= (running_status
<< 13) | (free_ca_mode
<< 12) |
340 (q
- desc_list_len_ptr
- 2);
341 desc_list_len_ptr
[0] = val
>> 8;
342 desc_list_len_ptr
[1] = val
;
344 mpegts_write_section1(&ts
->sdt
, SDT_TID
, ts
->tsid
, 0, 0, 0,
348 static MpegTSService
*mpegts_add_service(MpegTSWrite
*ts
,
350 const char *provider_name
,
353 MpegTSService
*service
;
355 service
= av_mallocz(sizeof(MpegTSService
));
358 service
->pmt
.pid
= DEFAULT_PMT_START_PID
+ ts
->nb_services
- 1;
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
);
367 static void section_write_packet(MpegTSSection
*s
, const uint8_t *packet
)
369 AVFormatContext
*ctx
= s
->opaque
;
370 put_buffer(ctx
->pb
, packet
, TS_PACKET_SIZE
);
373 static int mpegts_write_header(AVFormatContext
*s
)
375 MpegTSWrite
*ts
= s
->priv_data
;
376 MpegTSWriteStream
*ts_st
;
377 MpegTSService
*service
;
379 int i
, total_bit_rate
;
380 const char *service_name
;
382 ts
->tsid
= DEFAULT_TSID
;
383 ts
->onid
= DEFAULT_ONID
;
384 /* allocate a single DVB service */
385 service_name
= s
->title
;
386 if (service_name
[0] == '\0')
387 service_name
= DEFAULT_SERVICE_NAME
;
388 service
= mpegts_add_service(ts
, DEFAULT_SID
,
389 DEFAULT_PROVIDER_NAME
, service_name
);
390 service
->pmt
.write_packet
= section_write_packet
;
391 service
->pmt
.opaque
= s
;
393 ts
->pat
.pid
= PAT_PID
;
395 ts
->pat
.write_packet
= section_write_packet
;
398 ts
->sdt
.pid
= SDT_PID
;
400 ts
->sdt
.write_packet
= section_write_packet
;
403 /* assign pids to each stream */
405 for(i
= 0;i
< s
->nb_streams
; i
++) {
407 ts_st
= av_mallocz(sizeof(MpegTSWriteStream
));
410 st
->priv_data
= ts_st
;
411 ts_st
->service
= service
;
412 ts_st
->pid
= DEFAULT_START_PID
+ i
;
413 ts_st
->payload_pts
= AV_NOPTS_VALUE
;
414 ts_st
->payload_dts
= AV_NOPTS_VALUE
;
415 /* update PCR pid by using the first video stream */
416 if (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
&&
417 service
->pcr_pid
== 0x1fff)
418 service
->pcr_pid
= ts_st
->pid
;
419 total_bit_rate
+= st
->codec
->bit_rate
;
422 /* 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
;
428 if (total_bit_rate
<= 8 * 1024)
429 total_bit_rate
= 8 * 1024;
430 service
->pcr_packet_freq
= (total_bit_rate
* PCR_RETRANS_TIME
) /
431 (TS_PACKET_SIZE
* 8 * 1000);
432 ts
->sdt_packet_freq
= (total_bit_rate
* SDT_RETRANS_TIME
) /
433 (TS_PACKET_SIZE
* 8 * 1000);
434 ts
->pat_packet_freq
= (total_bit_rate
* PAT_RETRANS_TIME
) /
435 (TS_PACKET_SIZE
* 8 * 1000);
438 total_bit_rate
, ts
->sdt_packet_freq
, ts
->pat_packet_freq
);
441 /* write info at the start of the file, so that it will be fast to
445 for(i
= 0; i
< ts
->nb_services
; i
++) {
446 mpegts_write_pmt(s
, ts
->services
[i
]);
448 put_flush_packet(s
->pb
);
453 for(i
= 0;i
< s
->nb_streams
; i
++) {
455 av_free(st
->priv_data
);
460 /* send SDT, PAT and PMT tables regulary */
461 static void retransmit_si_info(AVFormatContext
*s
)
463 MpegTSWrite
*ts
= s
->priv_data
;
466 if (++ts
->sdt_packet_count
== ts
->sdt_packet_freq
) {
467 ts
->sdt_packet_count
= 0;
470 if (++ts
->pat_packet_count
== ts
->pat_packet_freq
) {
471 ts
->pat_packet_count
= 0;
473 for(i
= 0; i
< ts
->nb_services
; i
++) {
474 mpegts_write_pmt(s
, ts
->services
[i
]);
479 static void write_pts(uint8_t *q
, int fourbits
, int64_t pts
)
483 val
= fourbits
<< 4 | (((pts
>> 30) & 0x07) << 1) | 1;
485 val
= (((pts
>> 15) & 0x7fff) << 1) | 1;
488 val
= (((pts
) & 0x7fff) << 1) | 1;
493 /* 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 int64_t pts
, int64_t dts
)
498 MpegTSWriteStream
*ts_st
= st
->priv_data
;
499 uint8_t buf
[TS_PACKET_SIZE
];
501 int val
, is_start
, len
, header_len
, write_pcr
, private_code
, flags
;
502 int afc_len
, stuffing_len
;
503 int64_t pcr
= -1; /* avoid warning */
506 while (payload_size
> 0) {
507 retransmit_si_info(s
);
510 if (ts_st
->pid
== ts_st
->service
->pcr_pid
) {
511 ts_st
->service
->pcr_packet_count
++;
512 if (ts_st
->service
->pcr_packet_count
>=
513 ts_st
->service
->pcr_packet_freq
) {
514 ts_st
->service
->pcr_packet_count
= 0;
516 /* XXX: this is incorrect, but at least we have a PCR
522 /* prepare packet header */
525 val
= (ts_st
->pid
>> 8);
530 *q
++ = 0x10 | ts_st
->cc
| (write_pcr
? 0x20 : 0);
531 ts_st
->cc
= (ts_st
->cc
+ 1) & 0xf;
533 *q
++ = 7; /* AFC length */
534 *q
++ = 0x10; /* flags: PCR present */
539 *q
++ = (pcr
& 1) << 7;
543 int pes_extension
= 0;
544 /* write PES header */
549 if (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
) {
550 if (st
->codec
->codec_id
== CODEC_ID_DIRAC
) {
554 } 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
)) {
560 if (st
->codec
->codec_type
== CODEC_TYPE_SUBTITLE
) {
566 if (pts
!= AV_NOPTS_VALUE
) {
570 if (dts
!= AV_NOPTS_VALUE
) {
574 if (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
&&
575 st
->codec
->codec_id
== CODEC_ID_DIRAC
) {
576 /* set PES_extension_flag */
581 * One byte for PES2 extension flag +
582 * one byte for extension length +
583 * one byte for extension id
587 len
= payload_size
+ header_len
+ 3;
588 if (private_code
!= 0)
593 /* data alignment indicator is required for subtitle data */
594 if (st
->codec
->codec_type
== CODEC_TYPE_SUBTITLE
)
599 if (pts
!= AV_NOPTS_VALUE
) {
600 write_pts(q
, flags
>> 6, pts
);
603 if (dts
!= AV_NOPTS_VALUE
) {
604 write_pts(q
, 1, dts
);
607 if (pes_extension
&& st
->codec
->codec_id
== CODEC_ID_DIRAC
) {
608 flags
= 0x01; /* set PES_extension_flag_2 */
610 *q
++ = 0x80 | 0x01; /* marker bit + extension length */
612 * Set the stream id extension flag bit to 0 and
613 * write the extended stream id
617 if (private_code
!= 0)
622 header_len
= q
- buf
;
624 len
= TS_PACKET_SIZE
- header_len
;
625 if (len
> payload_size
)
627 stuffing_len
= TS_PACKET_SIZE
- header_len
- len
;
628 if (stuffing_len
> 0) {
629 /* add stuffing with AFC */
631 /* stuffing already present: increase its size */
632 afc_len
= buf
[4] + 1;
633 memmove(buf
+ 4 + afc_len
+ stuffing_len
,
635 header_len
- (4 + afc_len
));
636 buf
[4] += stuffing_len
;
637 memset(buf
+ 4 + afc_len
, 0xff, stuffing_len
);
640 memmove(buf
+ 4 + stuffing_len
, buf
+ 4, header_len
- 4);
642 buf
[4] = stuffing_len
- 1;
643 if (stuffing_len
>= 2) {
645 memset(buf
+ 6, 0xff, stuffing_len
- 2);
649 memcpy(buf
+ TS_PACKET_SIZE
- len
, payload
, len
);
652 put_buffer(s
->pb
, buf
, TS_PACKET_SIZE
);
654 put_flush_packet(s
->pb
);
657 static int mpegts_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
659 AVStream
*st
= s
->streams
[pkt
->stream_index
];
661 uint8_t *buf
= pkt
->data
;
662 MpegTSWriteStream
*ts_st
= st
->priv_data
;
663 int len
, max_payload_size
;
665 if (st
->codec
->codec_type
== CODEC_TYPE_SUBTITLE
) {
666 /* for subtitle, a single PES packet must be generated */
667 mpegts_write_pes(s
, st
, buf
, size
, pkt
->pts
, AV_NOPTS_VALUE
);
671 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
);
676 max_payload_size
= DEFAULT_PES_PAYLOAD_SIZE
;
678 len
= max_payload_size
- ts_st
->payload_index
;
681 memcpy(ts_st
->payload
+ ts_st
->payload_index
, buf
, len
);
684 ts_st
->payload_index
+= len
;
685 if (ts_st
->payload_pts
== AV_NOPTS_VALUE
)
686 ts_st
->payload_pts
= pkt
->pts
;
687 if (ts_st
->payload_dts
== AV_NOPTS_VALUE
)
688 ts_st
->payload_dts
= pkt
->dts
;
689 if (ts_st
->payload_index
>= max_payload_size
) {
690 mpegts_write_pes(s
, st
, ts_st
->payload
, ts_st
->payload_index
,
691 ts_st
->payload_pts
, ts_st
->payload_dts
);
692 ts_st
->payload_pts
= AV_NOPTS_VALUE
;
693 ts_st
->payload_dts
= AV_NOPTS_VALUE
;
694 ts_st
->payload_index
= 0;
700 static int mpegts_write_end(AVFormatContext
*s
)
702 MpegTSWrite
*ts
= s
->priv_data
;
703 MpegTSWriteStream
*ts_st
;
704 MpegTSService
*service
;
708 /* flush current packets */
709 for(i
= 0; i
< s
->nb_streams
; i
++) {
711 ts_st
= st
->priv_data
;
712 if (ts_st
->payload_index
> 0) {
713 mpegts_write_pes(s
, st
, ts_st
->payload
, ts_st
->payload_index
,
714 ts_st
->payload_pts
, ts_st
->payload_dts
);
717 put_flush_packet(s
->pb
);
719 for(i
= 0; i
< ts
->nb_services
; i
++) {
720 service
= ts
->services
[i
];
721 av_freep(&service
->provider_name
);
722 av_freep(&service
->name
);
725 av_free(ts
->services
);
730 AVOutputFormat mpegts_muxer
= {
732 NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),