Use tables symetry to reduce their size by half.
[ffmpeg-lucabe.git] / libavformat / mpegtsenc.c
blobfadbb0c59127d390a9559883214bf71f891c39b4
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_DIRAC:
224 stream_type = STREAM_TYPE_VIDEO_DIRAC;
225 break;
226 case CODEC_ID_MP2:
227 case CODEC_ID_MP3:
228 stream_type = STREAM_TYPE_AUDIO_MPEG1;
229 break;
230 case CODEC_ID_AAC:
231 stream_type = STREAM_TYPE_AUDIO_AAC;
232 break;
233 case CODEC_ID_AC3:
234 stream_type = STREAM_TYPE_AUDIO_AC3;
235 break;
236 default:
237 stream_type = STREAM_TYPE_PRIVATE_DATA;
238 break;
240 *q++ = stream_type;
241 put16(&q, 0xe000 | ts_st->pid);
242 desc_length_ptr = q;
243 q += 2; /* patched after */
245 /* write optional descriptors here */
246 switch(st->codec->codec_type) {
247 case CODEC_TYPE_AUDIO:
248 if (strlen(st->language) == 3) {
249 *q++ = 0x0a; /* ISO 639 language descriptor */
250 *q++ = 4;
251 *q++ = st->language[0];
252 *q++ = st->language[1];
253 *q++ = st->language[2];
254 *q++ = 0; /* undefined type */
256 break;
257 case CODEC_TYPE_SUBTITLE:
259 const char *language;
260 language = st->language;
261 if (strlen(language) != 3)
262 language = "eng";
263 *q++ = 0x59;
264 *q++ = 8;
265 *q++ = language[0];
266 *q++ = language[1];
267 *q++ = language[2];
268 *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
269 put16(&q, 1); /* page id */
270 put16(&q, 1); /* ancillary page id */
272 break;
273 case CODEC_TYPE_VIDEO:
274 if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
275 *q++ = 0x05; /*MPEG-2 registration descriptor*/
276 *q++ = 4;
277 *q++ = 'd';
278 *q++ = 'r';
279 *q++ = 'a';
280 *q++ = 'c';
282 break;
285 val = 0xf000 | (q - desc_length_ptr - 2);
286 desc_length_ptr[0] = val >> 8;
287 desc_length_ptr[1] = val;
289 mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
290 data, q - data);
293 /* NOTE: str == NULL is accepted for an empty string */
294 static void putstr8(uint8_t **q_ptr, const char *str)
296 uint8_t *q;
297 int len;
299 q = *q_ptr;
300 if (!str)
301 len = 0;
302 else
303 len = strlen(str);
304 *q++ = len;
305 memcpy(q, str, len);
306 q += len;
307 *q_ptr = q;
310 static void mpegts_write_sdt(AVFormatContext *s)
312 MpegTSWrite *ts = s->priv_data;
313 MpegTSService *service;
314 uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
315 int i, running_status, free_ca_mode, val;
317 q = data;
318 put16(&q, ts->onid);
319 *q++ = 0xff;
320 for(i = 0; i < ts->nb_services; i++) {
321 service = ts->services[i];
322 put16(&q, service->sid);
323 *q++ = 0xfc | 0x00; /* currently no EIT info */
324 desc_list_len_ptr = q;
325 q += 2;
326 running_status = 4; /* running */
327 free_ca_mode = 0;
329 /* write only one descriptor for the service name and provider */
330 *q++ = 0x48;
331 desc_len_ptr = q;
332 q++;
333 *q++ = 0x01; /* digital television service */
334 putstr8(&q, service->provider_name);
335 putstr8(&q, service->name);
336 desc_len_ptr[0] = q - desc_len_ptr - 1;
338 /* fill descriptor length */
339 val = (running_status << 13) | (free_ca_mode << 12) |
340 (q - desc_list_len_ptr - 2);
341 desc_list_len_ptr[0] = val >> 8;
342 desc_list_len_ptr[1] = val;
344 mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, 0, 0, 0,
345 data, q - data);
348 static MpegTSService *mpegts_add_service(MpegTSWrite *ts,
349 int sid,
350 const char *provider_name,
351 const char *name)
353 MpegTSService *service;
355 service = av_mallocz(sizeof(MpegTSService));
356 if (!service)
357 return NULL;
358 service->pmt.pid = DEFAULT_PMT_START_PID + ts->nb_services - 1;
359 service->sid = sid;
360 service->provider_name = av_strdup(provider_name);
361 service->name = av_strdup(name);
362 service->pcr_pid = 0x1fff;
363 dynarray_add(&ts->services, &ts->nb_services, service);
364 return service;
367 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
369 AVFormatContext *ctx = s->opaque;
370 put_buffer(ctx->pb, packet, TS_PACKET_SIZE);
373 static int mpegts_write_header(AVFormatContext *s)
375 MpegTSWrite *ts = s->priv_data;
376 MpegTSWriteStream *ts_st;
377 MpegTSService *service;
378 AVStream *st;
379 int i, total_bit_rate;
380 const char *service_name;
382 ts->tsid = DEFAULT_TSID;
383 ts->onid = DEFAULT_ONID;
384 /* allocate a single DVB service */
385 service_name = s->title;
386 if (service_name[0] == '\0')
387 service_name = DEFAULT_SERVICE_NAME;
388 service = mpegts_add_service(ts, DEFAULT_SID,
389 DEFAULT_PROVIDER_NAME, service_name);
390 service->pmt.write_packet = section_write_packet;
391 service->pmt.opaque = s;
393 ts->pat.pid = PAT_PID;
394 ts->pat.cc = 0;
395 ts->pat.write_packet = section_write_packet;
396 ts->pat.opaque = s;
398 ts->sdt.pid = SDT_PID;
399 ts->sdt.cc = 0;
400 ts->sdt.write_packet = section_write_packet;
401 ts->sdt.opaque = s;
403 /* assign pids to each stream */
404 total_bit_rate = 0;
405 for(i = 0;i < s->nb_streams; i++) {
406 st = s->streams[i];
407 ts_st = av_mallocz(sizeof(MpegTSWriteStream));
408 if (!ts_st)
409 goto fail;
410 st->priv_data = ts_st;
411 ts_st->service = service;
412 ts_st->pid = DEFAULT_START_PID + i;
413 ts_st->payload_pts = AV_NOPTS_VALUE;
414 ts_st->payload_dts = AV_NOPTS_VALUE;
415 /* update PCR pid by using the first video stream */
416 if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
417 service->pcr_pid == 0x1fff)
418 service->pcr_pid = ts_st->pid;
419 total_bit_rate += st->codec->bit_rate;
422 /* if no video stream, use the first stream as PCR */
423 if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
424 ts_st = s->streams[0]->priv_data;
425 service->pcr_pid = ts_st->pid;
428 if (total_bit_rate <= 8 * 1024)
429 total_bit_rate = 8 * 1024;
430 service->pcr_packet_freq = (total_bit_rate * PCR_RETRANS_TIME) /
431 (TS_PACKET_SIZE * 8 * 1000);
432 ts->sdt_packet_freq = (total_bit_rate * SDT_RETRANS_TIME) /
433 (TS_PACKET_SIZE * 8 * 1000);
434 ts->pat_packet_freq = (total_bit_rate * PAT_RETRANS_TIME) /
435 (TS_PACKET_SIZE * 8 * 1000);
436 #if 0
437 printf("%d %d %d\n",
438 total_bit_rate, ts->sdt_packet_freq, ts->pat_packet_freq);
439 #endif
441 /* write info at the start of the file, so that it will be fast to
442 find them */
443 mpegts_write_sdt(s);
444 mpegts_write_pat(s);
445 for(i = 0; i < ts->nb_services; i++) {
446 mpegts_write_pmt(s, ts->services[i]);
448 put_flush_packet(s->pb);
450 return 0;
452 fail:
453 for(i = 0;i < s->nb_streams; i++) {
454 st = s->streams[i];
455 av_free(st->priv_data);
457 return -1;
460 /* send SDT, PAT and PMT tables regulary */
461 static void retransmit_si_info(AVFormatContext *s)
463 MpegTSWrite *ts = s->priv_data;
464 int i;
466 if (++ts->sdt_packet_count == ts->sdt_packet_freq) {
467 ts->sdt_packet_count = 0;
468 mpegts_write_sdt(s);
470 if (++ts->pat_packet_count == ts->pat_packet_freq) {
471 ts->pat_packet_count = 0;
472 mpegts_write_pat(s);
473 for(i = 0; i < ts->nb_services; i++) {
474 mpegts_write_pmt(s, ts->services[i]);
479 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
481 int val;
483 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
484 *q++ = val;
485 val = (((pts >> 15) & 0x7fff) << 1) | 1;
486 *q++ = val >> 8;
487 *q++ = val;
488 val = (((pts) & 0x7fff) << 1) | 1;
489 *q++ = val >> 8;
490 *q++ = val;
493 /* NOTE: pes_data contains all the PES packet */
494 static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
495 const uint8_t *payload, int payload_size,
496 int64_t pts, int64_t dts)
498 MpegTSWriteStream *ts_st = st->priv_data;
499 uint8_t buf[TS_PACKET_SIZE];
500 uint8_t *q;
501 int val, is_start, len, header_len, write_pcr, private_code, flags;
502 int afc_len, stuffing_len;
503 int64_t pcr = -1; /* avoid warning */
505 is_start = 1;
506 while (payload_size > 0) {
507 retransmit_si_info(s);
509 write_pcr = 0;
510 if (ts_st->pid == ts_st->service->pcr_pid) {
511 ts_st->service->pcr_packet_count++;
512 if (ts_st->service->pcr_packet_count >=
513 ts_st->service->pcr_packet_freq) {
514 ts_st->service->pcr_packet_count = 0;
515 write_pcr = 1;
516 /* XXX: this is incorrect, but at least we have a PCR
517 value */
518 pcr = pts;
522 /* prepare packet header */
523 q = buf;
524 *q++ = 0x47;
525 val = (ts_st->pid >> 8);
526 if (is_start)
527 val |= 0x40;
528 *q++ = val;
529 *q++ = ts_st->pid;
530 *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
531 ts_st->cc = (ts_st->cc + 1) & 0xf;
532 if (write_pcr) {
533 *q++ = 7; /* AFC length */
534 *q++ = 0x10; /* flags: PCR present */
535 *q++ = pcr >> 25;
536 *q++ = pcr >> 17;
537 *q++ = pcr >> 9;
538 *q++ = pcr >> 1;
539 *q++ = (pcr & 1) << 7;
540 *q++ = 0;
542 if (is_start) {
543 int pes_extension = 0;
544 /* write PES header */
545 *q++ = 0x00;
546 *q++ = 0x00;
547 *q++ = 0x01;
548 private_code = 0;
549 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
550 if (st->codec->codec_id == CODEC_ID_DIRAC) {
551 *q++ = 0xfd;
552 } else
553 *q++ = 0xe0;
554 } else if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
555 (st->codec->codec_id == CODEC_ID_MP2 ||
556 st->codec->codec_id == CODEC_ID_MP3)) {
557 *q++ = 0xc0;
558 } else {
559 *q++ = 0xbd;
560 if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
561 private_code = 0x20;
564 header_len = 0;
565 flags = 0;
566 if (pts != AV_NOPTS_VALUE) {
567 header_len += 5;
568 flags |= 0x80;
570 if (dts != AV_NOPTS_VALUE) {
571 header_len += 5;
572 flags |= 0x40;
574 if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
575 st->codec->codec_id == CODEC_ID_DIRAC) {
576 /* set PES_extension_flag */
577 pes_extension = 1;
578 flags |= 0x01;
581 * One byte for PES2 extension flag +
582 * one byte for extension length +
583 * one byte for extension id
585 header_len += 3;
587 len = payload_size + header_len + 3;
588 if (private_code != 0)
589 len++;
590 *q++ = len >> 8;
591 *q++ = len;
592 val = 0x80;
593 /* data alignment indicator is required for subtitle data */
594 if (st->codec->codec_type == CODEC_TYPE_SUBTITLE)
595 val |= 0x04;
596 *q++ = val;
597 *q++ = flags;
598 *q++ = header_len;
599 if (pts != AV_NOPTS_VALUE) {
600 write_pts(q, flags >> 6, pts);
601 q += 5;
603 if (dts != AV_NOPTS_VALUE) {
604 write_pts(q, 1, dts);
605 q += 5;
607 if (pes_extension && st->codec->codec_id == CODEC_ID_DIRAC) {
608 flags = 0x01; /* set PES_extension_flag_2 */
609 *q++ = flags;
610 *q++ = 0x80 | 0x01; /* marker bit + extension length */
612 * Set the stream id extension flag bit to 0 and
613 * write the extended stream id
615 *q++ = 0x00 | 0x60;
617 if (private_code != 0)
618 *q++ = private_code;
619 is_start = 0;
621 /* header size */
622 header_len = q - buf;
623 /* data len */
624 len = TS_PACKET_SIZE - header_len;
625 if (len > payload_size)
626 len = payload_size;
627 stuffing_len = TS_PACKET_SIZE - header_len - len;
628 if (stuffing_len > 0) {
629 /* add stuffing with AFC */
630 if (buf[3] & 0x20) {
631 /* stuffing already present: increase its size */
632 afc_len = buf[4] + 1;
633 memmove(buf + 4 + afc_len + stuffing_len,
634 buf + 4 + afc_len,
635 header_len - (4 + afc_len));
636 buf[4] += stuffing_len;
637 memset(buf + 4 + afc_len, 0xff, stuffing_len);
638 } else {
639 /* add stuffing */
640 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
641 buf[3] |= 0x20;
642 buf[4] = stuffing_len - 1;
643 if (stuffing_len >= 2) {
644 buf[5] = 0x00;
645 memset(buf + 6, 0xff, stuffing_len - 2);
649 memcpy(buf + TS_PACKET_SIZE - len, payload, len);
650 payload += len;
651 payload_size -= len;
652 put_buffer(s->pb, buf, TS_PACKET_SIZE);
654 put_flush_packet(s->pb);
657 static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
659 AVStream *st = s->streams[pkt->stream_index];
660 int size= pkt->size;
661 uint8_t *buf= pkt->data;
662 MpegTSWriteStream *ts_st = st->priv_data;
663 int len, max_payload_size;
665 if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
666 /* for subtitle, a single PES packet must be generated */
667 mpegts_write_pes(s, st, buf, size, pkt->pts, AV_NOPTS_VALUE);
668 return 0;
671 if (st->codec->codec_id == CODEC_ID_DIRAC) {
672 /* for Dirac, a single PES packet must be generated */
673 mpegts_write_pes(s, st, buf, size, pkt->pts, pkt->dts);
674 return 0;
676 max_payload_size = DEFAULT_PES_PAYLOAD_SIZE;
677 while (size > 0) {
678 len = max_payload_size - ts_st->payload_index;
679 if (len > size)
680 len = size;
681 memcpy(ts_st->payload + ts_st->payload_index, buf, len);
682 buf += len;
683 size -= len;
684 ts_st->payload_index += len;
685 if (ts_st->payload_pts == AV_NOPTS_VALUE)
686 ts_st->payload_pts = pkt->pts;
687 if (ts_st->payload_dts == AV_NOPTS_VALUE)
688 ts_st->payload_dts = pkt->dts;
689 if (ts_st->payload_index >= max_payload_size) {
690 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
691 ts_st->payload_pts, ts_st->payload_dts);
692 ts_st->payload_pts = AV_NOPTS_VALUE;
693 ts_st->payload_dts = AV_NOPTS_VALUE;
694 ts_st->payload_index = 0;
697 return 0;
700 static int mpegts_write_end(AVFormatContext *s)
702 MpegTSWrite *ts = s->priv_data;
703 MpegTSWriteStream *ts_st;
704 MpegTSService *service;
705 AVStream *st;
706 int i;
708 /* flush current packets */
709 for(i = 0; i < s->nb_streams; i++) {
710 st = s->streams[i];
711 ts_st = st->priv_data;
712 if (ts_st->payload_index > 0) {
713 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
714 ts_st->payload_pts, ts_st->payload_dts);
717 put_flush_packet(s->pb);
719 for(i = 0; i < ts->nb_services; i++) {
720 service = ts->services[i];
721 av_freep(&service->provider_name);
722 av_freep(&service->name);
723 av_free(service);
725 av_free(ts->services);
727 return 0;
730 AVOutputFormat mpegts_muxer = {
731 "mpegts",
732 NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
733 "video/x-mpegts",
734 "ts,m2t",
735 sizeof(MpegTSWrite),
736 CODEC_ID_MP2,
737 CODEC_ID_MPEG2VIDEO,
738 mpegts_write_header,
739 mpegts_write_packet,
740 mpegts_write_end,