Remove debug line.
[FFMpeg-mirror/ordered_chapters.git] / libavformat / mpegtsenc.c
blob39868bea48b6fa46795011640e128ce7f610893d
1 /*
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
21 #include "avformat.h"
22 #include "crc.h"
23 #include "mpegts.h"
25 /* write DVB SI sections */
27 /*********************************************/
28 /* mpegts section writer */
30 typedef struct MpegTSSection {
31 int pid;
32 int cc;
33 void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
34 void *opaque;
35 } MpegTSSection;
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)
40 unsigned int crc;
41 unsigned char packet[TS_PACKET_SIZE];
42 const unsigned char *buf_ptr;
43 unsigned char *q;
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 */
53 buf_ptr = buf;
54 while (len > 0) {
55 first = (buf == buf_ptr);
56 q = packet;
57 *q++ = 0x47;
58 b = (s->pid >> 8);
59 if (first)
60 b |= 0x40;
61 *q++ = b;
62 *q++ = s->pid;
63 s->cc = (s->cc + 1) & 0xf;
64 *q++ = 0x10 | s->cc;
65 if (first)
66 *q++ = 0; /* 0 offset */
67 len1 = TS_PACKET_SIZE - (q - packet);
68 if (len1 > len)
69 len1 = len;
70 memcpy(q, buf_ptr, len1);
71 q += len1;
72 /* add known padding data */
73 left = TS_PACKET_SIZE - (q - packet);
74 if (left > 0)
75 memset(q, 0xff, left);
77 s->write_packet(s, packet);
79 buf_ptr += len1;
80 len -= len1;
84 static inline void put16(uint8_t **q_ptr, int val)
86 uint8_t *q;
87 q = *q_ptr;
88 *q++ = val >> 8;
89 *q++ = val;
90 *q_ptr = q;
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;
98 unsigned int tot_len;
100 tot_len = 3 + 5 + len + 4;
101 /* check if not too big */
102 if (tot_len > 1024)
103 return -1;
105 q = section;
106 *q++ = tid;
107 put16(&q, 0xb000 | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
108 put16(&q, id);
109 *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
110 *q++ = sec_num;
111 *q++ = last_sec_num;
112 memcpy(q, buf, len);
114 mpegts_write_section(s, section, tot_len);
115 return 0;
118 /*********************************************/
119 /* mpegts writer */
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 */
143 int cc;
144 int payload_index;
145 int64_t payload_pts;
146 uint8_t payload[DEFAULT_PES_PAYLOAD_SIZE];
147 } MpegTSWriteStream;
149 typedef struct MpegTSService {
150 MpegTSSection pmt; /* MPEG2 pmt table context */
151 int sid; /* service ID */
152 char *name;
153 char *provider_name;
154 int pcr_pid;
155 int pcr_packet_count;
156 int pcr_packet_freq;
157 } MpegTSService;
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;
164 int sdt_packet_freq;
165 int pat_packet_count;
166 int pat_packet_freq;
167 int nb_services;
168 int onid;
169 int tsid;
170 } MpegTSWrite;
172 static void mpegts_write_pat(AVFormatContext *s)
174 MpegTSWrite *ts = s->priv_data;
175 MpegTSService *service;
176 uint8_t data[1012], *q;
177 int i;
179 q = data;
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,
186 data, q - data);
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;
195 q = data;
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;
214 break;
215 case CODEC_ID_MPEG4:
216 stream_type = STREAM_TYPE_VIDEO_MPEG4;
217 break;
218 case CODEC_ID_H264:
219 stream_type = STREAM_TYPE_VIDEO_H264;
220 break;
221 case CODEC_ID_MP2:
222 case CODEC_ID_MP3:
223 stream_type = STREAM_TYPE_AUDIO_MPEG1;
224 break;
225 case CODEC_ID_AAC:
226 stream_type = STREAM_TYPE_AUDIO_AAC;
227 break;
228 case CODEC_ID_AC3:
229 stream_type = STREAM_TYPE_AUDIO_AC3;
230 break;
231 default:
232 stream_type = STREAM_TYPE_PRIVATE_DATA;
233 break;
235 *q++ = stream_type;
236 put16(&q, 0xe000 | ts_st->pid);
237 desc_length_ptr = q;
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 */
245 *q++ = 4;
246 *q++ = st->language[0];
247 *q++ = st->language[1];
248 *q++ = st->language[2];
249 *q++ = 0; /* undefined type */
251 break;
252 case CODEC_TYPE_SUBTITLE:
254 const char *language;
255 language = st->language;
256 if (strlen(language) != 3)
257 language = "eng";
258 *q++ = 0x59;
259 *q++ = 8;
260 *q++ = language[0];
261 *q++ = language[1];
262 *q++ = language[2];
263 *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
264 put16(&q, 1); /* page id */
265 put16(&q, 1); /* ancillary page id */
267 break;
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,
275 data, q - data);
278 /* NOTE: str == NULL is accepted for an empty string */
279 static void putstr8(uint8_t **q_ptr, const char *str)
281 uint8_t *q;
282 int len;
284 q = *q_ptr;
285 if (!str)
286 len = 0;
287 else
288 len = strlen(str);
289 *q++ = len;
290 memcpy(q, str, len);
291 q += len;
292 *q_ptr = q;
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;
302 q = data;
303 put16(&q, ts->onid);
304 *q++ = 0xff;
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;
310 q += 2;
311 running_status = 4; /* running */
312 free_ca_mode = 0;
314 /* write only one descriptor for the service name and provider */
315 *q++ = 0x48;
316 desc_len_ptr = q;
317 q++;
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,
330 data, q - data);
333 static MpegTSService *mpegts_add_service(MpegTSWrite *ts,
334 int sid,
335 const char *provider_name,
336 const char *name)
338 MpegTSService *service;
340 service = av_mallocz(sizeof(MpegTSService));
341 if (!service)
342 return NULL;
343 service->pmt.pid = DEFAULT_PMT_START_PID + ts->nb_services - 1;
344 service->sid = sid;
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);
349 return 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;
363 AVStream *st;
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;
379 ts->pat.cc = 0;
380 ts->pat.write_packet = section_write_packet;
381 ts->pat.opaque = s;
383 ts->sdt.pid = SDT_PID;
384 ts->sdt.cc = 0;
385 ts->sdt.write_packet = section_write_packet;
386 ts->sdt.opaque = s;
388 /* assign pids to each stream */
389 total_bit_rate = 0;
390 for(i = 0;i < s->nb_streams; i++) {
391 st = s->streams[i];
392 ts_st = av_mallocz(sizeof(MpegTSWriteStream));
393 if (!ts_st)
394 goto fail;
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);
420 #if 0
421 printf("%d %d %d\n",
422 total_bit_rate, ts->sdt_packet_freq, ts->pat_packet_freq);
423 #endif
425 /* write info at the start of the file, so that it will be fast to
426 find them */
427 mpegts_write_sdt(s);
428 mpegts_write_pat(s);
429 for(i = 0; i < ts->nb_services; i++) {
430 mpegts_write_pmt(s, ts->services[i]);
432 put_flush_packet(&s->pb);
434 return 0;
436 fail:
437 for(i = 0;i < s->nb_streams; i++) {
438 st = s->streams[i];
439 av_free(st->priv_data);
441 return -1;
444 /* send SDT, PAT and PMT tables regulary */
445 static void retransmit_si_info(AVFormatContext *s)
447 MpegTSWrite *ts = s->priv_data;
448 int i;
450 if (++ts->sdt_packet_count == ts->sdt_packet_freq) {
451 ts->sdt_packet_count = 0;
452 mpegts_write_sdt(s);
454 if (++ts->pat_packet_count == ts->pat_packet_freq) {
455 ts->pat_packet_count = 0;
456 mpegts_write_pat(s);
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,
466 int64_t pts)
468 MpegTSWriteStream *ts_st = st->priv_data;
469 uint8_t buf[TS_PACKET_SIZE];
470 uint8_t *q;
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 */
475 is_start = 1;
476 while (payload_size > 0) {
477 retransmit_si_info(s);
479 write_pcr = 0;
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;
485 write_pcr = 1;
486 /* XXX: this is incorrect, but at least we have a PCR
487 value */
488 pcr = pts;
492 /* prepare packet header */
493 q = buf;
494 *q++ = 0x47;
495 val = (ts_st->pid >> 8);
496 if (is_start)
497 val |= 0x40;
498 *q++ = val;
499 *q++ = ts_st->pid;
500 *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
501 ts_st->cc = (ts_st->cc + 1) & 0xf;
502 if (write_pcr) {
503 *q++ = 7; /* AFC length */
504 *q++ = 0x10; /* flags: PCR present */
505 *q++ = pcr >> 25;
506 *q++ = pcr >> 17;
507 *q++ = pcr >> 9;
508 *q++ = pcr >> 1;
509 *q++ = (pcr & 1) << 7;
510 *q++ = 0;
512 if (is_start) {
513 /* write PES header */
514 *q++ = 0x00;
515 *q++ = 0x00;
516 *q++ = 0x01;
517 private_code = 0;
518 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
519 *q++ = 0xe0;
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)) {
523 *q++ = 0xc0;
524 } else {
525 *q++ = 0xbd;
526 if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
527 private_code = 0x20;
530 if (pts != AV_NOPTS_VALUE)
531 header_len = 8;
532 else
533 header_len = 3;
534 if (private_code != 0)
535 header_len++;
536 len = payload_size + header_len;
537 *q++ = len >> 8;
538 *q++ = len;
539 val = 0x80;
540 /* data alignment indicator is required for subtitle data */
541 if (st->codec->codec_type == CODEC_TYPE_SUBTITLE)
542 val |= 0x04;
543 *q++ = val;
544 if (pts != AV_NOPTS_VALUE) {
545 *q++ = 0x80; /* PTS only */
546 *q++ = 0x05; /* header len */
547 val = (0x02 << 4) |
548 (((pts >> 30) & 0x07) << 1) | 1;
549 *q++ = val;
550 val = (((pts >> 15) & 0x7fff) << 1) | 1;
551 *q++ = val >> 8;
552 *q++ = val;
553 val = (((pts) & 0x7fff) << 1) | 1;
554 *q++ = val >> 8;
555 *q++ = val;
556 } else {
557 *q++ = 0x00;
558 *q++ = 0x00;
560 if (private_code != 0)
561 *q++ = private_code;
562 is_start = 0;
564 /* header size */
565 header_len = q - buf;
566 /* data len */
567 len = TS_PACKET_SIZE - header_len;
568 if (len > payload_size)
569 len = payload_size;
570 stuffing_len = TS_PACKET_SIZE - header_len - len;
571 if (stuffing_len > 0) {
572 /* add stuffing with AFC */
573 if (buf[3] & 0x20) {
574 /* stuffing already present: increase its size */
575 afc_len = buf[4] + 1;
576 memmove(buf + 4 + afc_len + stuffing_len,
577 buf + 4 + afc_len,
578 header_len - (4 + afc_len));
579 buf[4] += stuffing_len;
580 memset(buf + 4 + afc_len, 0xff, stuffing_len);
581 } else {
582 /* add stuffing */
583 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
584 buf[3] |= 0x20;
585 buf[4] = stuffing_len - 1;
586 if (stuffing_len >= 2) {
587 buf[5] = 0x00;
588 memset(buf + 6, 0xff, stuffing_len - 2);
592 memcpy(buf + TS_PACKET_SIZE - len, payload, len);
593 payload += len;
594 payload_size -= 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];
603 int size= pkt->size;
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);
611 return 0;
614 max_payload_size = DEFAULT_PES_PAYLOAD_SIZE;
615 while (size > 0) {
616 len = max_payload_size - ts_st->payload_index;
617 if (len > size)
618 len = size;
619 memcpy(ts_st->payload + ts_st->payload_index, buf, len);
620 buf += len;
621 size -= 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,
627 ts_st->payload_pts);
628 ts_st->payload_pts = AV_NOPTS_VALUE;
629 ts_st->payload_index = 0;
632 return 0;
635 static int mpegts_write_end(AVFormatContext *s)
637 MpegTSWrite *ts = s->priv_data;
638 MpegTSWriteStream *ts_st;
639 MpegTSService *service;
640 AVStream *st;
641 int i;
643 /* flush current packets */
644 for(i = 0; i < s->nb_streams; i++) {
645 st = s->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,
649 ts_st->payload_pts);
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);
658 av_free(service);
660 av_free(ts->services);
662 return 0;
665 AVOutputFormat mpegts_muxer = {
666 "mpegts",
667 "MPEG2 transport stream format",
668 "video/x-mpegts",
669 "ts",
670 sizeof(MpegTSWrite),
671 CODEC_ID_MP2,
672 CODEC_ID_MPEG2VIDEO,
673 mpegts_write_header,
674 mpegts_write_packet,
675 mpegts_write_end,