2 * MPEG2 transport stream (aka DVB) mux
3 * Copyright (c) 2003 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 /* write DVB SI sections */
26 /*********************************************/
27 /* mpegts section writer */
29 typedef struct MpegTSSection {
32 void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
36 /* NOTE: 4 bytes must be left at the end for the crc32 */
37 static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
40 unsigned char packet[TS_PACKET_SIZE];
41 const unsigned char *buf_ptr;
43 int first, b, len1, left;
45 crc = bswap_32(av_crc(av_crc04C11DB7, -1, buf, len - 4));
46 buf[len - 4] = (crc >> 24) & 0xff;
47 buf[len - 3] = (crc >> 16) & 0xff;
48 buf[len - 2] = (crc >> 8) & 0xff;
49 buf[len - 1] = (crc) & 0xff;
51 /* send each packet */
54 first = (buf == buf_ptr);
62 s->cc = (s->cc + 1) & 0xf;
65 *q++ = 0; /* 0 offset */
66 len1 = TS_PACKET_SIZE - (q - packet);
69 memcpy(q, buf_ptr, len1);
71 /* add known padding data */
72 left = TS_PACKET_SIZE - (q - packet);
74 memset(q, 0xff, left);
76 s->write_packet(s, packet);
83 static inline void put16(uint8_t **q_ptr, int val)
92 static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
93 int version, int sec_num, int last_sec_num,
94 uint8_t *buf, int len)
96 uint8_t section[1024], *q;
99 tot_len = 3 + 5 + len + 4;
100 /* check if not too big */
106 put16(&q, 0xb000 | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
108 *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
113 mpegts_write_section(s, section, tot_len);
117 /*********************************************/
120 #define DEFAULT_PMT_START_PID 0x1000
121 #define DEFAULT_START_PID 0x0100
122 #define DEFAULT_PROVIDER_NAME "FFmpeg"
123 #define DEFAULT_SERVICE_NAME "Service01"
125 /* default network id, transport stream and service identifiers */
126 #define DEFAULT_ONID 0x0001
127 #define DEFAULT_TSID 0x0001
128 #define DEFAULT_SID 0x0001
130 /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
131 #define DEFAULT_PES_HEADER_FREQ 16
132 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
134 /* we retransmit the SI info at this rate */
135 #define SDT_RETRANS_TIME 500
136 #define PAT_RETRANS_TIME 100
137 #define PCR_RETRANS_TIME 20
139 typedef struct MpegTSWriteStream {
140 struct MpegTSService *service;
141 int pid; /* stream associated pid */
145 uint8_t payload[DEFAULT_PES_PAYLOAD_SIZE];
148 typedef struct MpegTSService {
149 MpegTSSection pmt; /* MPEG2 pmt table context */
150 int sid; /* service ID */
154 int pcr_packet_count;
158 typedef struct MpegTSWrite {
159 MpegTSSection pat; /* MPEG2 pat table */
160 MpegTSSection sdt; /* MPEG2 sdt table context */
161 MpegTSService **services;
162 int sdt_packet_count;
164 int pat_packet_count;
171 static void mpegts_write_pat(AVFormatContext *s)
173 MpegTSWrite *ts = s->priv_data;
174 MpegTSService *service;
175 uint8_t data[1012], *q;
179 for(i = 0; i < ts->nb_services; i++) {
180 service = ts->services[i];
181 put16(&q, service->sid);
182 put16(&q, 0xe000 | service->pmt.pid);
184 mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, 0, 0, 0,
188 static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
190 // MpegTSWrite *ts = s->priv_data;
191 uint8_t data[1012], *q, *desc_length_ptr, *program_info_length_ptr;
192 int val, stream_type, i;
195 put16(&q, 0xe000 | service->pcr_pid);
197 program_info_length_ptr = q;
198 q += 2; /* patched after */
200 /* put program info here */
202 val = 0xf000 | (q - program_info_length_ptr - 2);
203 program_info_length_ptr[0] = val >> 8;
204 program_info_length_ptr[1] = val;
206 for(i = 0; i < s->nb_streams; i++) {
207 AVStream *st = s->streams[i];
208 MpegTSWriteStream *ts_st = st->priv_data;
209 switch(st->codec->codec_id) {
210 case CODEC_ID_MPEG1VIDEO:
211 case CODEC_ID_MPEG2VIDEO:
212 stream_type = STREAM_TYPE_VIDEO_MPEG2;
215 stream_type = STREAM_TYPE_VIDEO_MPEG4;
218 stream_type = STREAM_TYPE_VIDEO_H264;
222 stream_type = STREAM_TYPE_AUDIO_MPEG1;
225 stream_type = STREAM_TYPE_AUDIO_AAC;
228 stream_type = STREAM_TYPE_AUDIO_AC3;
231 stream_type = STREAM_TYPE_PRIVATE_DATA;
235 put16(&q, 0xe000 | ts_st->pid);
237 q += 2; /* patched after */
239 /* write optional descriptors here */
240 switch(st->codec->codec_type) {
241 case CODEC_TYPE_AUDIO:
242 if (strlen(st->language) == 3) {
243 *q++ = 0x0a; /* ISO 639 language descriptor */
245 *q++ = st->language[0];
246 *q++ = st->language[1];
247 *q++ = st->language[2];
248 *q++ = 0; /* undefined type */
251 case CODEC_TYPE_SUBTITLE:
253 const char *language;
254 language = st->language;
255 if (strlen(language) != 3)
262 *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
263 put16(&q, 1); /* page id */
264 put16(&q, 1); /* ancillary page id */
269 val = 0xf000 | (q - desc_length_ptr - 2);
270 desc_length_ptr[0] = val >> 8;
271 desc_length_ptr[1] = val;
273 mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
277 /* NOTE: str == NULL is accepted for an empty string */
278 static void putstr8(uint8_t **q_ptr, const char *str)
294 static void mpegts_write_sdt(AVFormatContext *s)
296 MpegTSWrite *ts = s->priv_data;
297 MpegTSService *service;
298 uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
299 int i, running_status, free_ca_mode, val;
304 for(i = 0; i < ts->nb_services; i++) {
305 service = ts->services[i];
306 put16(&q, service->sid);
307 *q++ = 0xfc | 0x00; /* currently no EIT info */
308 desc_list_len_ptr = q;
310 running_status = 4; /* running */
313 /* write only one descriptor for the service name and provider */
317 *q++ = 0x01; /* digital television service */
318 putstr8(&q, service->provider_name);
319 putstr8(&q, service->name);
320 desc_len_ptr[0] = q - desc_len_ptr - 1;
322 /* fill descriptor length */
323 val = (running_status << 13) | (free_ca_mode << 12) |
324 (q - desc_list_len_ptr - 2);
325 desc_list_len_ptr[0] = val >> 8;
326 desc_list_len_ptr[1] = val;
328 mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, 0, 0, 0,
332 static MpegTSService *mpegts_add_service(MpegTSWrite *ts,
334 const char *provider_name,
337 MpegTSService *service;
339 service = av_mallocz(sizeof(MpegTSService));
342 service->pmt.pid = DEFAULT_PMT_START_PID + ts->nb_services - 1;
344 service->provider_name = av_strdup(provider_name);
345 service->name = av_strdup(name);
346 service->pcr_pid = 0x1fff;
347 dynarray_add(&ts->services, &ts->nb_services, service);
351 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
353 AVFormatContext *ctx = s->opaque;
354 put_buffer(&ctx->pb, packet, TS_PACKET_SIZE);
357 static int mpegts_write_header(AVFormatContext *s)
359 MpegTSWrite *ts = s->priv_data;
360 MpegTSWriteStream *ts_st;
361 MpegTSService *service;
363 int i, total_bit_rate;
364 const char *service_name;
366 ts->tsid = DEFAULT_TSID;
367 ts->onid = DEFAULT_ONID;
368 /* allocate a single DVB service */
369 service_name = s->title;
370 if (service_name[0] == '\0')
371 service_name = DEFAULT_SERVICE_NAME;
372 service = mpegts_add_service(ts, DEFAULT_SID,
373 DEFAULT_PROVIDER_NAME, service_name);
374 service->pmt.write_packet = section_write_packet;
375 service->pmt.opaque = s;
377 ts->pat.pid = PAT_PID;
379 ts->pat.write_packet = section_write_packet;
382 ts->sdt.pid = SDT_PID;
384 ts->sdt.write_packet = section_write_packet;
387 /* assign pids to each stream */
389 for(i = 0;i < s->nb_streams; i++) {
391 ts_st = av_mallocz(sizeof(MpegTSWriteStream));
394 st->priv_data = ts_st;
395 ts_st->service = service;
396 ts_st->pid = DEFAULT_START_PID + i;
397 ts_st->payload_pts = AV_NOPTS_VALUE;
398 /* update PCR pid by using the first video stream */
399 if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
400 service->pcr_pid == 0x1fff)
401 service->pcr_pid = ts_st->pid;
402 total_bit_rate += st->codec->bit_rate;
405 /* if no video stream, use the first stream as PCR */
406 if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
407 ts_st = s->streams[0]->priv_data;
408 service->pcr_pid = ts_st->pid;
411 if (total_bit_rate <= 8 * 1024)
412 total_bit_rate = 8 * 1024;
413 service->pcr_packet_freq = (total_bit_rate * PCR_RETRANS_TIME) /
414 (TS_PACKET_SIZE * 8 * 1000);
415 ts->sdt_packet_freq = (total_bit_rate * SDT_RETRANS_TIME) /
416 (TS_PACKET_SIZE * 8 * 1000);
417 ts->pat_packet_freq = (total_bit_rate * PAT_RETRANS_TIME) /
418 (TS_PACKET_SIZE * 8 * 1000);
421 total_bit_rate, ts->sdt_packet_freq, ts->pat_packet_freq);
424 /* write info at the start of the file, so that it will be fast to
428 for(i = 0; i < ts->nb_services; i++) {
429 mpegts_write_pmt(s, ts->services[i]);
431 put_flush_packet(&s->pb);
436 for(i = 0;i < s->nb_streams; i++) {
438 av_free(st->priv_data);
443 /* send SDT, PAT and PMT tables regulary */
444 static void retransmit_si_info(AVFormatContext *s)
446 MpegTSWrite *ts = s->priv_data;
449 if (++ts->sdt_packet_count == ts->sdt_packet_freq) {
450 ts->sdt_packet_count = 0;
453 if (++ts->pat_packet_count == ts->pat_packet_freq) {
454 ts->pat_packet_count = 0;
456 for(i = 0; i < ts->nb_services; i++) {
457 mpegts_write_pmt(s, ts->services[i]);
462 /* NOTE: pes_data contains all the PES packet */
463 static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
464 const uint8_t *payload, int payload_size,
467 MpegTSWriteStream *ts_st = st->priv_data;
468 uint8_t buf[TS_PACKET_SIZE];
470 int val, is_start, len, header_len, write_pcr, private_code;
471 int afc_len, stuffing_len;
472 int64_t pcr = -1; /* avoid warning */
475 while (payload_size > 0) {
476 retransmit_si_info(s);
479 if (ts_st->pid == ts_st->service->pcr_pid) {
480 ts_st->service->pcr_packet_count++;
481 if (ts_st->service->pcr_packet_count >=
482 ts_st->service->pcr_packet_freq) {
483 ts_st->service->pcr_packet_count = 0;
485 /* XXX: this is incorrect, but at least we have a PCR
491 /* prepare packet header */
494 val = (ts_st->pid >> 8);
499 *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
500 ts_st->cc = (ts_st->cc + 1) & 0xf;
502 *q++ = 7; /* AFC length */
503 *q++ = 0x10; /* flags: PCR present */
508 *q++ = (pcr & 1) << 7;
512 /* write PES header */
517 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
519 } else if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
520 (st->codec->codec_id == CODEC_ID_MP2 ||
521 st->codec->codec_id == CODEC_ID_MP3)) {
525 if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
529 if (pts != AV_NOPTS_VALUE)
533 if (private_code != 0)
535 len = payload_size + header_len;
539 /* data alignment indicator is required for subtitle data */
540 if (st->codec->codec_type == CODEC_TYPE_SUBTITLE)
543 if (pts != AV_NOPTS_VALUE) {
544 *q++ = 0x80; /* PTS only */
545 *q++ = 0x05; /* header len */
547 (((pts >> 30) & 0x07) << 1) | 1;
549 val = (((pts >> 15) & 0x7fff) << 1) | 1;
552 val = (((pts) & 0x7fff) << 1) | 1;
559 if (private_code != 0)
564 header_len = q - buf;
566 len = TS_PACKET_SIZE - header_len;
567 if (len > payload_size)
569 stuffing_len = TS_PACKET_SIZE - header_len - len;
570 if (stuffing_len > 0) {
571 /* add stuffing with AFC */
573 /* stuffing already present: increase its size */
574 afc_len = buf[4] + 1;
575 memmove(buf + 4 + afc_len + stuffing_len,
577 header_len - (4 + afc_len));
578 buf[4] += stuffing_len;
579 memset(buf + 4 + afc_len, 0xff, stuffing_len);
582 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
584 buf[4] = stuffing_len - 1;
585 if (stuffing_len >= 2) {
587 memset(buf + 6, 0xff, stuffing_len - 2);
591 memcpy(buf + TS_PACKET_SIZE - len, payload, len);
594 put_buffer(&s->pb, buf, TS_PACKET_SIZE);
596 put_flush_packet(&s->pb);
599 static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
601 AVStream *st = s->streams[pkt->stream_index];
603 uint8_t *buf= pkt->data;
604 MpegTSWriteStream *ts_st = st->priv_data;
605 int len, max_payload_size;
607 if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
608 /* for subtitle, a single PES packet must be generated */
609 mpegts_write_pes(s, st, buf, size, pkt->pts);
613 max_payload_size = DEFAULT_PES_PAYLOAD_SIZE;
615 len = max_payload_size - ts_st->payload_index;
618 memcpy(ts_st->payload + ts_st->payload_index, buf, len);
621 ts_st->payload_index += len;
622 if (ts_st->payload_pts == AV_NOPTS_VALUE)
623 ts_st->payload_pts = pkt->pts;
624 if (ts_st->payload_index >= max_payload_size) {
625 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
627 ts_st->payload_pts = AV_NOPTS_VALUE;
628 ts_st->payload_index = 0;
634 static int mpegts_write_end(AVFormatContext *s)
636 MpegTSWrite *ts = s->priv_data;
637 MpegTSWriteStream *ts_st;
638 MpegTSService *service;
642 /* flush current packets */
643 for(i = 0; i < s->nb_streams; i++) {
645 ts_st = st->priv_data;
646 if (ts_st->payload_index > 0) {
647 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
651 put_flush_packet(&s->pb);
653 for(i = 0; i < ts->nb_services; i++) {
654 service = ts->services[i];
655 av_freep(&service->provider_name);
656 av_freep(&service->name);
659 av_free(ts->services);
664 AVOutputFormat mpegts_muxer = {
666 "MPEG2 transport stream format",
676 #endif // CONFIG_MUXERS