Make ff_mlp_read_major_sync() take a GetBitContext instead of buffers.
[FFMpeg-mirror/DVCPRO-HD.git] / libavformat / mpegtsenc.c
blob4a70a15f74500e03135c88caa9c9dbef24b759ce
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
22 #include "libavutil/crc.h"
23 #include "avformat.h"
24 #include "mpegts.h"
26 /* write DVB SI sections */
28 /*********************************************/
29 /* mpegts section writer */
31 typedef struct MpegTSSection {
32 int pid;
33 int cc;
34 void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
35 void *opaque;
36 } MpegTSSection;
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)
41 unsigned int crc;
42 unsigned char packet[TS_PACKET_SIZE];
43 const unsigned char *buf_ptr;
44 unsigned char *q;
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 */
54 buf_ptr = buf;
55 while (len > 0) {
56 first = (buf == buf_ptr);
57 q = packet;
58 *q++ = 0x47;
59 b = (s->pid >> 8);
60 if (first)
61 b |= 0x40;
62 *q++ = b;
63 *q++ = s->pid;
64 *q++ = 0x10 | s->cc;
65 s->cc = (s->cc + 1) & 0xf;
66 if (first)
67 *q++ = 0; /* 0 offset */
68 len1 = TS_PACKET_SIZE - (q - packet);
69 if (len1 > len)
70 len1 = len;
71 memcpy(q, buf_ptr, len1);
72 q += len1;
73 /* add known padding data */
74 left = TS_PACKET_SIZE - (q - packet);
75 if (left > 0)
76 memset(q, 0xff, left);
78 s->write_packet(s, packet);
80 buf_ptr += len1;
81 len -= len1;
85 static inline void put16(uint8_t **q_ptr, int val)
87 uint8_t *q;
88 q = *q_ptr;
89 *q++ = val >> 8;
90 *q++ = val;
91 *q_ptr = q;
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;
99 unsigned int tot_len;
101 tot_len = 3 + 5 + len + 4;
102 /* check if not too big */
103 if (tot_len > 1024)
104 return -1;
106 q = section;
107 *q++ = tid;
108 put16(&q, 0xb000 | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
109 put16(&q, id);
110 *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
111 *q++ = sec_num;
112 *q++ = last_sec_num;
113 memcpy(q, buf, len);
115 mpegts_write_section(s, section, tot_len);
116 return 0;
119 /*********************************************/
120 /* mpegts writer */
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 */
144 int cc;
145 int payload_index;
146 int64_t payload_pts;
147 int64_t payload_dts;
148 uint8_t payload[DEFAULT_PES_PAYLOAD_SIZE];
149 } MpegTSWriteStream;
151 typedef struct MpegTSService {
152 MpegTSSection pmt; /* MPEG2 pmt table context */
153 int sid; /* service ID */
154 char *name;
155 char *provider_name;
156 int pcr_pid;
157 int pcr_packet_count;
158 int pcr_packet_freq;
159 } MpegTSService;
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;
166 int sdt_packet_freq;
167 int pat_packet_count;
168 int pat_packet_freq;
169 int nb_services;
170 int onid;
171 int tsid;
172 } MpegTSWrite;
174 static void mpegts_write_pat(AVFormatContext *s)
176 MpegTSWrite *ts = s->priv_data;
177 MpegTSService *service;
178 uint8_t data[1012], *q;
179 int i;
181 q = data;
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,
188 data, q - data);
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;
197 q = data;
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;
216 break;
217 case CODEC_ID_MPEG4:
218 stream_type = STREAM_TYPE_VIDEO_MPEG4;
219 break;
220 case CODEC_ID_H264:
221 stream_type = STREAM_TYPE_VIDEO_H264;
222 break;
223 case CODEC_ID_MP2:
224 case CODEC_ID_MP3:
225 stream_type = STREAM_TYPE_AUDIO_MPEG1;
226 break;
227 case CODEC_ID_AAC:
228 stream_type = STREAM_TYPE_AUDIO_AAC;
229 break;
230 case CODEC_ID_AC3:
231 stream_type = STREAM_TYPE_AUDIO_AC3;
232 break;
233 default:
234 stream_type = STREAM_TYPE_PRIVATE_DATA;
235 break;
237 *q++ = stream_type;
238 put16(&q, 0xe000 | ts_st->pid);
239 desc_length_ptr = q;
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 */
247 *q++ = 4;
248 *q++ = st->language[0];
249 *q++ = st->language[1];
250 *q++ = st->language[2];
251 *q++ = 0; /* undefined type */
253 break;
254 case CODEC_TYPE_SUBTITLE:
256 const char *language;
257 language = st->language;
258 if (strlen(language) != 3)
259 language = "eng";
260 *q++ = 0x59;
261 *q++ = 8;
262 *q++ = language[0];
263 *q++ = language[1];
264 *q++ = language[2];
265 *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
266 put16(&q, 1); /* page id */
267 put16(&q, 1); /* ancillary page id */
269 break;
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,
277 data, q - data);
280 /* NOTE: str == NULL is accepted for an empty string */
281 static void putstr8(uint8_t **q_ptr, const char *str)
283 uint8_t *q;
284 int len;
286 q = *q_ptr;
287 if (!str)
288 len = 0;
289 else
290 len = strlen(str);
291 *q++ = len;
292 memcpy(q, str, len);
293 q += len;
294 *q_ptr = q;
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;
304 q = data;
305 put16(&q, ts->onid);
306 *q++ = 0xff;
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;
312 q += 2;
313 running_status = 4; /* running */
314 free_ca_mode = 0;
316 /* write only one descriptor for the service name and provider */
317 *q++ = 0x48;
318 desc_len_ptr = q;
319 q++;
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,
332 data, q - data);
335 static MpegTSService *mpegts_add_service(MpegTSWrite *ts,
336 int sid,
337 const char *provider_name,
338 const char *name)
340 MpegTSService *service;
342 service = av_mallocz(sizeof(MpegTSService));
343 if (!service)
344 return NULL;
345 service->pmt.pid = DEFAULT_PMT_START_PID + ts->nb_services - 1;
346 service->sid = sid;
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);
351 return 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;
365 AVStream *st;
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;
381 ts->pat.cc = 0;
382 ts->pat.write_packet = section_write_packet;
383 ts->pat.opaque = s;
385 ts->sdt.pid = SDT_PID;
386 ts->sdt.cc = 0;
387 ts->sdt.write_packet = section_write_packet;
388 ts->sdt.opaque = s;
390 /* assign pids to each stream */
391 total_bit_rate = 0;
392 for(i = 0;i < s->nb_streams; i++) {
393 st = s->streams[i];
394 ts_st = av_mallocz(sizeof(MpegTSWriteStream));
395 if (!ts_st)
396 goto fail;
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);
423 #if 0
424 printf("%d %d %d\n",
425 total_bit_rate, ts->sdt_packet_freq, ts->pat_packet_freq);
426 #endif
428 /* write info at the start of the file, so that it will be fast to
429 find them */
430 mpegts_write_sdt(s);
431 mpegts_write_pat(s);
432 for(i = 0; i < ts->nb_services; i++) {
433 mpegts_write_pmt(s, ts->services[i]);
435 put_flush_packet(s->pb);
437 return 0;
439 fail:
440 for(i = 0;i < s->nb_streams; i++) {
441 st = s->streams[i];
442 av_free(st->priv_data);
444 return -1;
447 /* send SDT, PAT and PMT tables regulary */
448 static void retransmit_si_info(AVFormatContext *s)
450 MpegTSWrite *ts = s->priv_data;
451 int i;
453 if (++ts->sdt_packet_count == ts->sdt_packet_freq) {
454 ts->sdt_packet_count = 0;
455 mpegts_write_sdt(s);
457 if (++ts->pat_packet_count == ts->pat_packet_freq) {
458 ts->pat_packet_count = 0;
459 mpegts_write_pat(s);
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)
468 int val;
470 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
471 *q++ = val;
472 val = (((pts >> 15) & 0x7fff) << 1) | 1;
473 *q++ = val >> 8;
474 *q++ = val;
475 val = (((pts) & 0x7fff) << 1) | 1;
476 *q++ = val >> 8;
477 *q++ = val;
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];
487 uint8_t *q;
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 */
492 is_start = 1;
493 while (payload_size > 0) {
494 retransmit_si_info(s);
496 write_pcr = 0;
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;
502 write_pcr = 1;
503 /* XXX: this is incorrect, but at least we have a PCR
504 value */
505 pcr = pts;
509 /* prepare packet header */
510 q = buf;
511 *q++ = 0x47;
512 val = (ts_st->pid >> 8);
513 if (is_start)
514 val |= 0x40;
515 *q++ = val;
516 *q++ = ts_st->pid;
517 *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
518 ts_st->cc = (ts_st->cc + 1) & 0xf;
519 if (write_pcr) {
520 *q++ = 7; /* AFC length */
521 *q++ = 0x10; /* flags: PCR present */
522 *q++ = pcr >> 25;
523 *q++ = pcr >> 17;
524 *q++ = pcr >> 9;
525 *q++ = pcr >> 1;
526 *q++ = (pcr & 1) << 7;
527 *q++ = 0;
529 if (is_start) {
530 /* write PES header */
531 *q++ = 0x00;
532 *q++ = 0x00;
533 *q++ = 0x01;
534 private_code = 0;
535 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
536 *q++ = 0xe0;
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)) {
540 *q++ = 0xc0;
541 } else {
542 *q++ = 0xbd;
543 if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
544 private_code = 0x20;
547 header_len = 0;
548 flags = 0;
549 if (pts != AV_NOPTS_VALUE) {
550 header_len += 5;
551 flags |= 0x80;
553 if (dts != AV_NOPTS_VALUE) {
554 header_len += 5;
555 flags |= 0x40;
557 len = payload_size + header_len + 3;
558 if (private_code != 0)
559 len++;
560 *q++ = len >> 8;
561 *q++ = len;
562 val = 0x80;
563 /* data alignment indicator is required for subtitle data */
564 if (st->codec->codec_type == CODEC_TYPE_SUBTITLE)
565 val |= 0x04;
566 *q++ = val;
567 *q++ = flags;
568 *q++ = header_len;
569 if (pts != AV_NOPTS_VALUE) {
570 write_pts(q, flags >> 6, pts);
571 q += 5;
573 if (dts != AV_NOPTS_VALUE) {
574 write_pts(q, 1, dts);
575 q += 5;
577 if (private_code != 0)
578 *q++ = private_code;
579 is_start = 0;
581 /* header size */
582 header_len = q - buf;
583 /* data len */
584 len = TS_PACKET_SIZE - header_len;
585 if (len > payload_size)
586 len = payload_size;
587 stuffing_len = TS_PACKET_SIZE - header_len - len;
588 if (stuffing_len > 0) {
589 /* add stuffing with AFC */
590 if (buf[3] & 0x20) {
591 /* stuffing already present: increase its size */
592 afc_len = buf[4] + 1;
593 memmove(buf + 4 + afc_len + stuffing_len,
594 buf + 4 + afc_len,
595 header_len - (4 + afc_len));
596 buf[4] += stuffing_len;
597 memset(buf + 4 + afc_len, 0xff, stuffing_len);
598 } else {
599 /* add stuffing */
600 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
601 buf[3] |= 0x20;
602 buf[4] = stuffing_len - 1;
603 if (stuffing_len >= 2) {
604 buf[5] = 0x00;
605 memset(buf + 6, 0xff, stuffing_len - 2);
609 memcpy(buf + TS_PACKET_SIZE - len, payload, len);
610 payload += len;
611 payload_size -= 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];
620 int size= pkt->size;
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);
628 return 0;
631 max_payload_size = DEFAULT_PES_PAYLOAD_SIZE;
632 while (size > 0) {
633 len = max_payload_size - ts_st->payload_index;
634 if (len > size)
635 len = size;
636 memcpy(ts_st->payload + ts_st->payload_index, buf, len);
637 buf += len;
638 size -= 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;
652 return 0;
655 static int mpegts_write_end(AVFormatContext *s)
657 MpegTSWrite *ts = s->priv_data;
658 MpegTSWriteStream *ts_st;
659 MpegTSService *service;
660 AVStream *st;
661 int i;
663 /* flush current packets */
664 for(i = 0; i < s->nb_streams; i++) {
665 st = s->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);
678 av_free(service);
680 av_free(ts->services);
682 return 0;
685 AVOutputFormat mpegts_muxer = {
686 "mpegts",
687 NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
688 "video/x-mpegts",
689 "ts,m2t",
690 sizeof(MpegTSWrite),
691 CODEC_ID_MP2,
692 CODEC_ID_MPEG2VIDEO,
693 mpegts_write_header,
694 mpegts_write_packet,
695 mpegts_write_end,