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
);
64 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
;
225 stream_type
= STREAM_TYPE_AUDIO_MPEG1
;
228 stream_type
= STREAM_TYPE_AUDIO_AAC
;
231 stream_type
= STREAM_TYPE_AUDIO_AC3
;
234 stream_type
= STREAM_TYPE_PRIVATE_DATA
;
238 put16(&q
, 0xe000 | ts_st
->pid
);
240 q
+= 2; /* patched after */
242 /* write optional descriptors here */
243 switch(st
->codec
->codec_type
) {
244 case CODEC_TYPE_AUDIO
:
245 if (strlen(st
->language
) == 3) {
246 *q
++ = 0x0a; /* ISO 639 language descriptor */
248 *q
++ = st
->language
[0];
249 *q
++ = st
->language
[1];
250 *q
++ = st
->language
[2];
251 *q
++ = 0; /* undefined type */
254 case CODEC_TYPE_SUBTITLE
:
256 const char *language
;
257 language
= st
->language
;
258 if (strlen(language
) != 3)
265 *q
++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
266 put16(&q
, 1); /* page id */
267 put16(&q
, 1); /* ancillary page id */
272 val
= 0xf000 | (q
- desc_length_ptr
- 2);
273 desc_length_ptr
[0] = val
>> 8;
274 desc_length_ptr
[1] = val
;
276 mpegts_write_section1(&service
->pmt
, PMT_TID
, service
->sid
, 0, 0, 0,
280 /* NOTE: str == NULL is accepted for an empty string */
281 static void putstr8(uint8_t **q_ptr
, const char *str
)
297 static void mpegts_write_sdt(AVFormatContext
*s
)
299 MpegTSWrite
*ts
= s
->priv_data
;
300 MpegTSService
*service
;
301 uint8_t data
[1012], *q
, *desc_list_len_ptr
, *desc_len_ptr
;
302 int i
, running_status
, free_ca_mode
, val
;
307 for(i
= 0; i
< ts
->nb_services
; i
++) {
308 service
= ts
->services
[i
];
309 put16(&q
, service
->sid
);
310 *q
++ = 0xfc | 0x00; /* currently no EIT info */
311 desc_list_len_ptr
= q
;
313 running_status
= 4; /* running */
316 /* write only one descriptor for the service name and provider */
320 *q
++ = 0x01; /* digital television service */
321 putstr8(&q
, service
->provider_name
);
322 putstr8(&q
, service
->name
);
323 desc_len_ptr
[0] = q
- desc_len_ptr
- 1;
325 /* fill descriptor length */
326 val
= (running_status
<< 13) | (free_ca_mode
<< 12) |
327 (q
- desc_list_len_ptr
- 2);
328 desc_list_len_ptr
[0] = val
>> 8;
329 desc_list_len_ptr
[1] = val
;
331 mpegts_write_section1(&ts
->sdt
, SDT_TID
, ts
->tsid
, 0, 0, 0,
335 static MpegTSService
*mpegts_add_service(MpegTSWrite
*ts
,
337 const char *provider_name
,
340 MpegTSService
*service
;
342 service
= av_mallocz(sizeof(MpegTSService
));
345 service
->pmt
.pid
= DEFAULT_PMT_START_PID
+ ts
->nb_services
- 1;
347 service
->provider_name
= av_strdup(provider_name
);
348 service
->name
= av_strdup(name
);
349 service
->pcr_pid
= 0x1fff;
350 dynarray_add(&ts
->services
, &ts
->nb_services
, service
);
354 static void section_write_packet(MpegTSSection
*s
, const uint8_t *packet
)
356 AVFormatContext
*ctx
= s
->opaque
;
357 put_buffer(ctx
->pb
, packet
, TS_PACKET_SIZE
);
360 static int mpegts_write_header(AVFormatContext
*s
)
362 MpegTSWrite
*ts
= s
->priv_data
;
363 MpegTSWriteStream
*ts_st
;
364 MpegTSService
*service
;
366 int i
, total_bit_rate
;
367 const char *service_name
;
369 ts
->tsid
= DEFAULT_TSID
;
370 ts
->onid
= DEFAULT_ONID
;
371 /* allocate a single DVB service */
372 service_name
= s
->title
;
373 if (service_name
[0] == '\0')
374 service_name
= DEFAULT_SERVICE_NAME
;
375 service
= mpegts_add_service(ts
, DEFAULT_SID
,
376 DEFAULT_PROVIDER_NAME
, service_name
);
377 service
->pmt
.write_packet
= section_write_packet
;
378 service
->pmt
.opaque
= s
;
380 ts
->pat
.pid
= PAT_PID
;
382 ts
->pat
.write_packet
= section_write_packet
;
385 ts
->sdt
.pid
= SDT_PID
;
387 ts
->sdt
.write_packet
= section_write_packet
;
390 /* assign pids to each stream */
392 for(i
= 0;i
< s
->nb_streams
; i
++) {
394 ts_st
= av_mallocz(sizeof(MpegTSWriteStream
));
397 st
->priv_data
= ts_st
;
398 ts_st
->service
= service
;
399 ts_st
->pid
= DEFAULT_START_PID
+ i
;
400 ts_st
->payload_pts
= AV_NOPTS_VALUE
;
401 ts_st
->payload_dts
= AV_NOPTS_VALUE
;
402 /* update PCR pid by using the first video stream */
403 if (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
&&
404 service
->pcr_pid
== 0x1fff)
405 service
->pcr_pid
= ts_st
->pid
;
406 total_bit_rate
+= st
->codec
->bit_rate
;
409 /* if no video stream, use the first stream as PCR */
410 if (service
->pcr_pid
== 0x1fff && s
->nb_streams
> 0) {
411 ts_st
= s
->streams
[0]->priv_data
;
412 service
->pcr_pid
= ts_st
->pid
;
415 if (total_bit_rate
<= 8 * 1024)
416 total_bit_rate
= 8 * 1024;
417 service
->pcr_packet_freq
= (total_bit_rate
* PCR_RETRANS_TIME
) /
418 (TS_PACKET_SIZE
* 8 * 1000);
419 ts
->sdt_packet_freq
= (total_bit_rate
* SDT_RETRANS_TIME
) /
420 (TS_PACKET_SIZE
* 8 * 1000);
421 ts
->pat_packet_freq
= (total_bit_rate
* PAT_RETRANS_TIME
) /
422 (TS_PACKET_SIZE
* 8 * 1000);
425 total_bit_rate
, ts
->sdt_packet_freq
, ts
->pat_packet_freq
);
428 /* write info at the start of the file, so that it will be fast to
432 for(i
= 0; i
< ts
->nb_services
; i
++) {
433 mpegts_write_pmt(s
, ts
->services
[i
]);
435 put_flush_packet(s
->pb
);
440 for(i
= 0;i
< s
->nb_streams
; i
++) {
442 av_free(st
->priv_data
);
447 /* send SDT, PAT and PMT tables regulary */
448 static void retransmit_si_info(AVFormatContext
*s
)
450 MpegTSWrite
*ts
= s
->priv_data
;
453 if (++ts
->sdt_packet_count
== ts
->sdt_packet_freq
) {
454 ts
->sdt_packet_count
= 0;
457 if (++ts
->pat_packet_count
== ts
->pat_packet_freq
) {
458 ts
->pat_packet_count
= 0;
460 for(i
= 0; i
< ts
->nb_services
; i
++) {
461 mpegts_write_pmt(s
, ts
->services
[i
]);
466 static void write_pts(uint8_t *q
, int fourbits
, int64_t pts
)
470 val
= fourbits
<< 4 | (((pts
>> 30) & 0x07) << 1) | 1;
472 val
= (((pts
>> 15) & 0x7fff) << 1) | 1;
475 val
= (((pts
) & 0x7fff) << 1) | 1;
480 /* NOTE: pes_data contains all the PES packet */
481 static void mpegts_write_pes(AVFormatContext
*s
, AVStream
*st
,
482 const uint8_t *payload
, int payload_size
,
483 int64_t pts
, int64_t dts
)
485 MpegTSWriteStream
*ts_st
= st
->priv_data
;
486 uint8_t buf
[TS_PACKET_SIZE
];
488 int val
, is_start
, len
, header_len
, write_pcr
, private_code
, flags
;
489 int afc_len
, stuffing_len
;
490 int64_t pcr
= -1; /* avoid warning */
493 while (payload_size
> 0) {
494 retransmit_si_info(s
);
497 if (ts_st
->pid
== ts_st
->service
->pcr_pid
) {
498 ts_st
->service
->pcr_packet_count
++;
499 if (ts_st
->service
->pcr_packet_count
>=
500 ts_st
->service
->pcr_packet_freq
) {
501 ts_st
->service
->pcr_packet_count
= 0;
503 /* XXX: this is incorrect, but at least we have a PCR
509 /* prepare packet header */
512 val
= (ts_st
->pid
>> 8);
517 *q
++ = 0x10 | ts_st
->cc
| (write_pcr
? 0x20 : 0);
518 ts_st
->cc
= (ts_st
->cc
+ 1) & 0xf;
520 *q
++ = 7; /* AFC length */
521 *q
++ = 0x10; /* flags: PCR present */
526 *q
++ = (pcr
& 1) << 7;
530 /* write PES header */
535 if (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
) {
537 } else if (st
->codec
->codec_type
== CODEC_TYPE_AUDIO
&&
538 (st
->codec
->codec_id
== CODEC_ID_MP2
||
539 st
->codec
->codec_id
== CODEC_ID_MP3
)) {
543 if (st
->codec
->codec_type
== CODEC_TYPE_SUBTITLE
) {
549 if (pts
!= AV_NOPTS_VALUE
) {
553 if (dts
!= AV_NOPTS_VALUE
) {
557 len
= payload_size
+ header_len
+ 3;
558 if (private_code
!= 0)
563 /* data alignment indicator is required for subtitle data */
564 if (st
->codec
->codec_type
== CODEC_TYPE_SUBTITLE
)
569 if (pts
!= AV_NOPTS_VALUE
) {
570 write_pts(q
, flags
>> 6, pts
);
573 if (dts
!= AV_NOPTS_VALUE
) {
574 write_pts(q
, 1, dts
);
577 if (private_code
!= 0)
582 header_len
= q
- buf
;
584 len
= TS_PACKET_SIZE
- header_len
;
585 if (len
> payload_size
)
587 stuffing_len
= TS_PACKET_SIZE
- header_len
- len
;
588 if (stuffing_len
> 0) {
589 /* add stuffing with AFC */
591 /* stuffing already present: increase its size */
592 afc_len
= buf
[4] + 1;
593 memmove(buf
+ 4 + afc_len
+ stuffing_len
,
595 header_len
- (4 + afc_len
));
596 buf
[4] += stuffing_len
;
597 memset(buf
+ 4 + afc_len
, 0xff, stuffing_len
);
600 memmove(buf
+ 4 + stuffing_len
, buf
+ 4, header_len
- 4);
602 buf
[4] = stuffing_len
- 1;
603 if (stuffing_len
>= 2) {
605 memset(buf
+ 6, 0xff, stuffing_len
- 2);
609 memcpy(buf
+ TS_PACKET_SIZE
- len
, payload
, len
);
612 put_buffer(s
->pb
, buf
, TS_PACKET_SIZE
);
614 put_flush_packet(s
->pb
);
617 static int mpegts_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
619 AVStream
*st
= s
->streams
[pkt
->stream_index
];
621 uint8_t *buf
= pkt
->data
;
622 MpegTSWriteStream
*ts_st
= st
->priv_data
;
623 int len
, max_payload_size
;
625 if (st
->codec
->codec_type
== CODEC_TYPE_SUBTITLE
) {
626 /* for subtitle, a single PES packet must be generated */
627 mpegts_write_pes(s
, st
, buf
, size
, pkt
->pts
, AV_NOPTS_VALUE
);
631 max_payload_size
= DEFAULT_PES_PAYLOAD_SIZE
;
633 len
= max_payload_size
- ts_st
->payload_index
;
636 memcpy(ts_st
->payload
+ ts_st
->payload_index
, buf
, len
);
639 ts_st
->payload_index
+= len
;
640 if (ts_st
->payload_pts
== AV_NOPTS_VALUE
)
641 ts_st
->payload_pts
= pkt
->pts
;
642 if (ts_st
->payload_dts
== AV_NOPTS_VALUE
)
643 ts_st
->payload_dts
= pkt
->dts
;
644 if (ts_st
->payload_index
>= max_payload_size
) {
645 mpegts_write_pes(s
, st
, ts_st
->payload
, ts_st
->payload_index
,
646 ts_st
->payload_pts
, ts_st
->payload_dts
);
647 ts_st
->payload_pts
= AV_NOPTS_VALUE
;
648 ts_st
->payload_dts
= AV_NOPTS_VALUE
;
649 ts_st
->payload_index
= 0;
655 static int mpegts_write_end(AVFormatContext
*s
)
657 MpegTSWrite
*ts
= s
->priv_data
;
658 MpegTSWriteStream
*ts_st
;
659 MpegTSService
*service
;
663 /* flush current packets */
664 for(i
= 0; i
< s
->nb_streams
; i
++) {
666 ts_st
= st
->priv_data
;
667 if (ts_st
->payload_index
> 0) {
668 mpegts_write_pes(s
, st
, ts_st
->payload
, ts_st
->payload_index
,
669 ts_st
->payload_pts
, ts_st
->payload_dts
);
672 put_flush_packet(s
->pb
);
674 for(i
= 0; i
< ts
->nb_services
; i
++) {
675 service
= ts
->services
[i
];
676 av_freep(&service
->provider_name
);
677 av_freep(&service
->name
);
680 av_free(ts
->services
);
685 AVOutputFormat mpegts_muxer
= {
687 "MPEG2 transport stream format",