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 */
146 uint8_t payload
[DEFAULT_PES_PAYLOAD_SIZE
];
149 typedef struct MpegTSService
{
150 MpegTSSection pmt
; /* MPEG2 pmt table context */
151 int sid
; /* service ID */
155 int pcr_packet_count
;
159 typedef struct MpegTSWrite
{
160 MpegTSSection pat
; /* MPEG2 pat table */
161 MpegTSSection sdt
; /* MPEG2 sdt table context */
162 MpegTSService
**services
;
163 int sdt_packet_count
;
165 int pat_packet_count
;
172 static void mpegts_write_pat(AVFormatContext
*s
)
174 MpegTSWrite
*ts
= s
->priv_data
;
175 MpegTSService
*service
;
176 uint8_t data
[1012], *q
;
180 for(i
= 0; i
< ts
->nb_services
; i
++) {
181 service
= ts
->services
[i
];
182 put16(&q
, service
->sid
);
183 put16(&q
, 0xe000 | service
->pmt
.pid
);
185 mpegts_write_section1(&ts
->pat
, PAT_TID
, ts
->tsid
, 0, 0, 0,
189 static void mpegts_write_pmt(AVFormatContext
*s
, MpegTSService
*service
)
191 // MpegTSWrite *ts = s->priv_data;
192 uint8_t data
[1012], *q
, *desc_length_ptr
, *program_info_length_ptr
;
193 int val
, stream_type
, i
;
196 put16(&q
, 0xe000 | service
->pcr_pid
);
198 program_info_length_ptr
= q
;
199 q
+= 2; /* patched after */
201 /* put program info here */
203 val
= 0xf000 | (q
- program_info_length_ptr
- 2);
204 program_info_length_ptr
[0] = val
>> 8;
205 program_info_length_ptr
[1] = val
;
207 for(i
= 0; i
< s
->nb_streams
; i
++) {
208 AVStream
*st
= s
->streams
[i
];
209 MpegTSWriteStream
*ts_st
= st
->priv_data
;
210 switch(st
->codec
->codec_id
) {
211 case CODEC_ID_MPEG1VIDEO
:
212 case CODEC_ID_MPEG2VIDEO
:
213 stream_type
= STREAM_TYPE_VIDEO_MPEG2
;
216 stream_type
= STREAM_TYPE_VIDEO_MPEG4
;
219 stream_type
= STREAM_TYPE_VIDEO_H264
;
223 stream_type
= STREAM_TYPE_AUDIO_MPEG1
;
226 stream_type
= STREAM_TYPE_AUDIO_AAC
;
229 stream_type
= STREAM_TYPE_AUDIO_AC3
;
232 stream_type
= STREAM_TYPE_PRIVATE_DATA
;
236 put16(&q
, 0xe000 | ts_st
->pid
);
238 q
+= 2; /* patched after */
240 /* write optional descriptors here */
241 switch(st
->codec
->codec_type
) {
242 case CODEC_TYPE_AUDIO
:
243 if (strlen(st
->language
) == 3) {
244 *q
++ = 0x0a; /* ISO 639 language descriptor */
246 *q
++ = st
->language
[0];
247 *q
++ = st
->language
[1];
248 *q
++ = st
->language
[2];
249 *q
++ = 0; /* undefined type */
252 case CODEC_TYPE_SUBTITLE
:
254 const char *language
;
255 language
= st
->language
;
256 if (strlen(language
) != 3)
263 *q
++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
264 put16(&q
, 1); /* page id */
265 put16(&q
, 1); /* ancillary page id */
270 val
= 0xf000 | (q
- desc_length_ptr
- 2);
271 desc_length_ptr
[0] = val
>> 8;
272 desc_length_ptr
[1] = val
;
274 mpegts_write_section1(&service
->pmt
, PMT_TID
, service
->sid
, 0, 0, 0,
278 /* NOTE: str == NULL is accepted for an empty string */
279 static void putstr8(uint8_t **q_ptr
, const char *str
)
295 static void mpegts_write_sdt(AVFormatContext
*s
)
297 MpegTSWrite
*ts
= s
->priv_data
;
298 MpegTSService
*service
;
299 uint8_t data
[1012], *q
, *desc_list_len_ptr
, *desc_len_ptr
;
300 int i
, running_status
, free_ca_mode
, val
;
305 for(i
= 0; i
< ts
->nb_services
; i
++) {
306 service
= ts
->services
[i
];
307 put16(&q
, service
->sid
);
308 *q
++ = 0xfc | 0x00; /* currently no EIT info */
309 desc_list_len_ptr
= q
;
311 running_status
= 4; /* running */
314 /* write only one descriptor for the service name and provider */
318 *q
++ = 0x01; /* digital television service */
319 putstr8(&q
, service
->provider_name
);
320 putstr8(&q
, service
->name
);
321 desc_len_ptr
[0] = q
- desc_len_ptr
- 1;
323 /* fill descriptor length */
324 val
= (running_status
<< 13) | (free_ca_mode
<< 12) |
325 (q
- desc_list_len_ptr
- 2);
326 desc_list_len_ptr
[0] = val
>> 8;
327 desc_list_len_ptr
[1] = val
;
329 mpegts_write_section1(&ts
->sdt
, SDT_TID
, ts
->tsid
, 0, 0, 0,
333 static MpegTSService
*mpegts_add_service(MpegTSWrite
*ts
,
335 const char *provider_name
,
338 MpegTSService
*service
;
340 service
= av_mallocz(sizeof(MpegTSService
));
343 service
->pmt
.pid
= DEFAULT_PMT_START_PID
+ ts
->nb_services
- 1;
345 service
->provider_name
= av_strdup(provider_name
);
346 service
->name
= av_strdup(name
);
347 service
->pcr_pid
= 0x1fff;
348 dynarray_add(&ts
->services
, &ts
->nb_services
, service
);
352 static void section_write_packet(MpegTSSection
*s
, const uint8_t *packet
)
354 AVFormatContext
*ctx
= s
->opaque
;
355 put_buffer(&ctx
->pb
, packet
, TS_PACKET_SIZE
);
358 static int mpegts_write_header(AVFormatContext
*s
)
360 MpegTSWrite
*ts
= s
->priv_data
;
361 MpegTSWriteStream
*ts_st
;
362 MpegTSService
*service
;
364 int i
, total_bit_rate
;
365 const char *service_name
;
367 ts
->tsid
= DEFAULT_TSID
;
368 ts
->onid
= DEFAULT_ONID
;
369 /* allocate a single DVB service */
370 service_name
= s
->title
;
371 if (service_name
[0] == '\0')
372 service_name
= DEFAULT_SERVICE_NAME
;
373 service
= mpegts_add_service(ts
, DEFAULT_SID
,
374 DEFAULT_PROVIDER_NAME
, service_name
);
375 service
->pmt
.write_packet
= section_write_packet
;
376 service
->pmt
.opaque
= s
;
378 ts
->pat
.pid
= PAT_PID
;
380 ts
->pat
.write_packet
= section_write_packet
;
383 ts
->sdt
.pid
= SDT_PID
;
385 ts
->sdt
.write_packet
= section_write_packet
;
388 /* assign pids to each stream */
390 for(i
= 0;i
< s
->nb_streams
; i
++) {
392 ts_st
= av_mallocz(sizeof(MpegTSWriteStream
));
395 st
->priv_data
= ts_st
;
396 ts_st
->service
= service
;
397 ts_st
->pid
= DEFAULT_START_PID
+ i
;
398 ts_st
->payload_pts
= AV_NOPTS_VALUE
;
399 /* update PCR pid by using the first video stream */
400 if (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
&&
401 service
->pcr_pid
== 0x1fff)
402 service
->pcr_pid
= ts_st
->pid
;
403 total_bit_rate
+= st
->codec
->bit_rate
;
406 /* if no video stream, use the first stream as PCR */
407 if (service
->pcr_pid
== 0x1fff && s
->nb_streams
> 0) {
408 ts_st
= s
->streams
[0]->priv_data
;
409 service
->pcr_pid
= ts_st
->pid
;
412 if (total_bit_rate
<= 8 * 1024)
413 total_bit_rate
= 8 * 1024;
414 service
->pcr_packet_freq
= (total_bit_rate
* PCR_RETRANS_TIME
) /
415 (TS_PACKET_SIZE
* 8 * 1000);
416 ts
->sdt_packet_freq
= (total_bit_rate
* SDT_RETRANS_TIME
) /
417 (TS_PACKET_SIZE
* 8 * 1000);
418 ts
->pat_packet_freq
= (total_bit_rate
* PAT_RETRANS_TIME
) /
419 (TS_PACKET_SIZE
* 8 * 1000);
422 total_bit_rate
, ts
->sdt_packet_freq
, ts
->pat_packet_freq
);
425 /* write info at the start of the file, so that it will be fast to
429 for(i
= 0; i
< ts
->nb_services
; i
++) {
430 mpegts_write_pmt(s
, ts
->services
[i
]);
432 put_flush_packet(&s
->pb
);
437 for(i
= 0;i
< s
->nb_streams
; i
++) {
439 av_free(st
->priv_data
);
444 /* send SDT, PAT and PMT tables regulary */
445 static void retransmit_si_info(AVFormatContext
*s
)
447 MpegTSWrite
*ts
= s
->priv_data
;
450 if (++ts
->sdt_packet_count
== ts
->sdt_packet_freq
) {
451 ts
->sdt_packet_count
= 0;
454 if (++ts
->pat_packet_count
== ts
->pat_packet_freq
) {
455 ts
->pat_packet_count
= 0;
457 for(i
= 0; i
< ts
->nb_services
; i
++) {
458 mpegts_write_pmt(s
, ts
->services
[i
]);
463 /* NOTE: pes_data contains all the PES packet */
464 static void mpegts_write_pes(AVFormatContext
*s
, AVStream
*st
,
465 const uint8_t *payload
, int payload_size
,
468 MpegTSWriteStream
*ts_st
= st
->priv_data
;
469 uint8_t buf
[TS_PACKET_SIZE
];
471 int val
, is_start
, len
, header_len
, write_pcr
, private_code
;
472 int afc_len
, stuffing_len
;
473 int64_t pcr
= -1; /* avoid warning */
476 while (payload_size
> 0) {
477 retransmit_si_info(s
);
480 if (ts_st
->pid
== ts_st
->service
->pcr_pid
) {
481 ts_st
->service
->pcr_packet_count
++;
482 if (ts_st
->service
->pcr_packet_count
>=
483 ts_st
->service
->pcr_packet_freq
) {
484 ts_st
->service
->pcr_packet_count
= 0;
486 /* XXX: this is incorrect, but at least we have a PCR
492 /* prepare packet header */
495 val
= (ts_st
->pid
>> 8);
500 *q
++ = 0x10 | ts_st
->cc
| (write_pcr
? 0x20 : 0);
501 ts_st
->cc
= (ts_st
->cc
+ 1) & 0xf;
503 *q
++ = 7; /* AFC length */
504 *q
++ = 0x10; /* flags: PCR present */
509 *q
++ = (pcr
& 1) << 7;
513 /* write PES header */
518 if (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
) {
520 } else if (st
->codec
->codec_type
== CODEC_TYPE_AUDIO
&&
521 (st
->codec
->codec_id
== CODEC_ID_MP2
||
522 st
->codec
->codec_id
== CODEC_ID_MP3
)) {
526 if (st
->codec
->codec_type
== CODEC_TYPE_SUBTITLE
) {
530 if (pts
!= AV_NOPTS_VALUE
)
534 if (private_code
!= 0)
536 len
= payload_size
+ header_len
;
540 /* data alignment indicator is required for subtitle data */
541 if (st
->codec
->codec_type
== CODEC_TYPE_SUBTITLE
)
544 if (pts
!= AV_NOPTS_VALUE
) {
545 *q
++ = 0x80; /* PTS only */
546 *q
++ = 0x05; /* header len */
548 (((pts
>> 30) & 0x07) << 1) | 1;
550 val
= (((pts
>> 15) & 0x7fff) << 1) | 1;
553 val
= (((pts
) & 0x7fff) << 1) | 1;
560 if (private_code
!= 0)
565 header_len
= q
- buf
;
567 len
= TS_PACKET_SIZE
- header_len
;
568 if (len
> payload_size
)
570 stuffing_len
= TS_PACKET_SIZE
- header_len
- len
;
571 if (stuffing_len
> 0) {
572 /* add stuffing with AFC */
574 /* stuffing already present: increase its size */
575 afc_len
= buf
[4] + 1;
576 memmove(buf
+ 4 + afc_len
+ stuffing_len
,
578 header_len
- (4 + afc_len
));
579 buf
[4] += stuffing_len
;
580 memset(buf
+ 4 + afc_len
, 0xff, stuffing_len
);
583 memmove(buf
+ 4 + stuffing_len
, buf
+ 4, header_len
- 4);
585 buf
[4] = stuffing_len
- 1;
586 if (stuffing_len
>= 2) {
588 memset(buf
+ 6, 0xff, stuffing_len
- 2);
592 memcpy(buf
+ TS_PACKET_SIZE
- len
, payload
, len
);
595 put_buffer(&s
->pb
, buf
, TS_PACKET_SIZE
);
597 put_flush_packet(&s
->pb
);
600 static int mpegts_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
602 AVStream
*st
= s
->streams
[pkt
->stream_index
];
604 uint8_t *buf
= pkt
->data
;
605 MpegTSWriteStream
*ts_st
= st
->priv_data
;
606 int len
, max_payload_size
;
608 if (st
->codec
->codec_type
== CODEC_TYPE_SUBTITLE
) {
609 /* for subtitle, a single PES packet must be generated */
610 mpegts_write_pes(s
, st
, buf
, size
, pkt
->pts
);
614 max_payload_size
= DEFAULT_PES_PAYLOAD_SIZE
;
616 len
= max_payload_size
- ts_st
->payload_index
;
619 memcpy(ts_st
->payload
+ ts_st
->payload_index
, buf
, len
);
622 ts_st
->payload_index
+= len
;
623 if (ts_st
->payload_pts
== AV_NOPTS_VALUE
)
624 ts_st
->payload_pts
= pkt
->pts
;
625 if (ts_st
->payload_index
>= max_payload_size
) {
626 mpegts_write_pes(s
, st
, ts_st
->payload
, ts_st
->payload_index
,
628 ts_st
->payload_pts
= AV_NOPTS_VALUE
;
629 ts_st
->payload_index
= 0;
635 static int mpegts_write_end(AVFormatContext
*s
)
637 MpegTSWrite
*ts
= s
->priv_data
;
638 MpegTSWriteStream
*ts_st
;
639 MpegTSService
*service
;
643 /* flush current packets */
644 for(i
= 0; i
< s
->nb_streams
; i
++) {
646 ts_st
= st
->priv_data
;
647 if (ts_st
->payload_index
> 0) {
648 mpegts_write_pes(s
, st
, ts_st
->payload
, ts_st
->payload_index
,
652 put_flush_packet(&s
->pb
);
654 for(i
= 0; i
< ts
->nb_services
; i
++) {
655 service
= ts
->services
[i
];
656 av_freep(&service
->provider_name
);
657 av_freep(&service
->name
);
660 av_free(ts
->services
);
665 AVOutputFormat mpegts_muxer
= {
667 "MPEG2 transport stream format",