00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/bswap.h"
00023 #include "libavutil/crc.h"
00024 #include "libavutil/dict.h"
00025 #include "libavutil/mathematics.h"
00026 #include "libavutil/opt.h"
00027 #include "libavutil/avassert.h"
00028 #include "libavcodec/mpegvideo.h"
00029 #include "avformat.h"
00030 #include "internal.h"
00031 #include "mpegts.h"
00032
00033 #define PCR_TIME_BASE 27000000
00034
00035
00036
00037
00038
00039
00040 typedef struct MpegTSSection {
00041 int pid;
00042 int cc;
00043 void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
00044 void *opaque;
00045 } MpegTSSection;
00046
00047 typedef struct MpegTSService {
00048 MpegTSSection pmt;
00049 int sid;
00050 char *name;
00051 char *provider_name;
00052 int pcr_pid;
00053 int pcr_packet_count;
00054 int pcr_packet_period;
00055 } MpegTSService;
00056
00057 typedef struct MpegTSWrite {
00058 const AVClass *av_class;
00059 MpegTSSection pat;
00060 MpegTSSection sdt;
00061 MpegTSService **services;
00062 int sdt_packet_count;
00063 int sdt_packet_period;
00064 int pat_packet_count;
00065 int pat_packet_period;
00066 int nb_services;
00067 int onid;
00068 int tsid;
00069 int64_t first_pcr;
00070 int mux_rate;
00071 int pes_payload_size;
00072
00073 int transport_stream_id;
00074 int original_network_id;
00075 int service_id;
00076
00077 int pmt_start_pid;
00078 int start_pid;
00079 int m2ts_mode;
00080
00081 int reemit_pat_pmt;
00082
00083 #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
00084 #define MPEGTS_FLAG_AAC_LATM 0x02
00085 int flags;
00086 int copyts;
00087 } MpegTSWrite;
00088
00089
00090 #define DEFAULT_PES_HEADER_FREQ 16
00091 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
00092
00093 static const AVOption options[] = {
00094 { "mpegts_transport_stream_id", "Set transport_stream_id field.",
00095 offsetof(MpegTSWrite, transport_stream_id), AV_OPT_TYPE_INT, {.i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
00096 { "mpegts_original_network_id", "Set original_network_id field.",
00097 offsetof(MpegTSWrite, original_network_id), AV_OPT_TYPE_INT, {.i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
00098 { "mpegts_service_id", "Set service_id field.",
00099 offsetof(MpegTSWrite, service_id), AV_OPT_TYPE_INT, {.i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
00100 { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
00101 offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT, {.i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM},
00102 { "mpegts_start_pid", "Set the first pid.",
00103 offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT, {.i64 = 0x0100 }, 0x0100, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM},
00104 {"mpegts_m2ts_mode", "Enable m2ts mode.",
00105 offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_INT, {.i64 = -1 },
00106 -1,1, AV_OPT_FLAG_ENCODING_PARAM},
00107 { "muxrate", NULL, offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT, {.i64 = 1}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
00108 { "pes_payload_size", "Minimum PES packet payload in bytes",
00109 offsetof(MpegTSWrite, pes_payload_size), AV_OPT_TYPE_INT, {.i64 = DEFAULT_PES_PAYLOAD_SIZE}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
00110 { "mpegts_flags", "MPEG-TS muxing flags", offsetof(MpegTSWrite, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX,
00111 AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
00112 { "resend_headers", "Reemit PAT/PMT before writing the next packet",
00113 0, AV_OPT_TYPE_CONST, {.i64 = MPEGTS_FLAG_REEMIT_PAT_PMT}, 0, INT_MAX,
00114 AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags"},
00115 { "latm", "Use LATM packetization for AAC",
00116 0, AV_OPT_TYPE_CONST, {.i64 = MPEGTS_FLAG_AAC_LATM}, 0, INT_MAX,
00117 AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags"},
00118
00119 { "resend_headers", "Reemit PAT/PMT before writing the next packet",
00120 offsetof(MpegTSWrite, reemit_pat_pmt), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
00121 { "mpegts_copyts", "dont offset dts/pts",
00122 offsetof(MpegTSWrite, copyts), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
00123 { NULL },
00124 };
00125
00126 static const AVClass mpegts_muxer_class = {
00127 .class_name = "MPEGTS muxer",
00128 .item_name = av_default_item_name,
00129 .option = options,
00130 .version = LIBAVUTIL_VERSION_INT,
00131 };
00132
00133
00134 static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
00135 {
00136 unsigned int crc;
00137 unsigned char packet[TS_PACKET_SIZE];
00138 const unsigned char *buf_ptr;
00139 unsigned char *q;
00140 int first, b, len1, left;
00141
00142 crc = av_bswap32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, buf, len - 4));
00143 buf[len - 4] = (crc >> 24) & 0xff;
00144 buf[len - 3] = (crc >> 16) & 0xff;
00145 buf[len - 2] = (crc >> 8) & 0xff;
00146 buf[len - 1] = (crc) & 0xff;
00147
00148
00149 buf_ptr = buf;
00150 while (len > 0) {
00151 first = (buf == buf_ptr);
00152 q = packet;
00153 *q++ = 0x47;
00154 b = (s->pid >> 8);
00155 if (first)
00156 b |= 0x40;
00157 *q++ = b;
00158 *q++ = s->pid;
00159 s->cc = (s->cc + 1) & 0xf;
00160 *q++ = 0x10 | s->cc;
00161 if (first)
00162 *q++ = 0;
00163 len1 = TS_PACKET_SIZE - (q - packet);
00164 if (len1 > len)
00165 len1 = len;
00166 memcpy(q, buf_ptr, len1);
00167 q += len1;
00168
00169 left = TS_PACKET_SIZE - (q - packet);
00170 if (left > 0)
00171 memset(q, 0xff, left);
00172
00173 s->write_packet(s, packet);
00174
00175 buf_ptr += len1;
00176 len -= len1;
00177 }
00178 }
00179
00180 static inline void put16(uint8_t **q_ptr, int val)
00181 {
00182 uint8_t *q;
00183 q = *q_ptr;
00184 *q++ = val >> 8;
00185 *q++ = val;
00186 *q_ptr = q;
00187 }
00188
00189 static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
00190 int version, int sec_num, int last_sec_num,
00191 uint8_t *buf, int len)
00192 {
00193 uint8_t section[1024], *q;
00194 unsigned int tot_len;
00195
00196 unsigned int flags = tid == SDT_TID ? 0xf000 : 0xb000;
00197
00198 tot_len = 3 + 5 + len + 4;
00199
00200 if (tot_len > 1024)
00201 return AVERROR_INVALIDDATA;
00202
00203 q = section;
00204 *q++ = tid;
00205 put16(&q, flags | (len + 5 + 4));
00206 put16(&q, id);
00207 *q++ = 0xc1 | (version << 1);
00208 *q++ = sec_num;
00209 *q++ = last_sec_num;
00210 memcpy(q, buf, len);
00211
00212 mpegts_write_section(s, section, tot_len);
00213 return 0;
00214 }
00215
00216
00217
00218
00219 #define DEFAULT_PROVIDER_NAME "FFmpeg"
00220 #define DEFAULT_SERVICE_NAME "Service01"
00221
00222
00223 #define SDT_RETRANS_TIME 500
00224 #define PAT_RETRANS_TIME 100
00225 #define PCR_RETRANS_TIME 20
00226
00227 typedef struct MpegTSWriteStream {
00228 struct MpegTSService *service;
00229 int pid;
00230 int cc;
00231 int payload_size;
00232 int first_pts_check;
00233 int prev_payload_key;
00234 int64_t payload_pts;
00235 int64_t payload_dts;
00236 int payload_flags;
00237 uint8_t *payload;
00238 AVFormatContext *amux;
00239 } MpegTSWriteStream;
00240
00241 static void mpegts_write_pat(AVFormatContext *s)
00242 {
00243 MpegTSWrite *ts = s->priv_data;
00244 MpegTSService *service;
00245 uint8_t data[1012], *q;
00246 int i;
00247
00248 q = data;
00249 for(i = 0; i < ts->nb_services; i++) {
00250 service = ts->services[i];
00251 put16(&q, service->sid);
00252 put16(&q, 0xe000 | service->pmt.pid);
00253 }
00254 mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, 0, 0, 0,
00255 data, q - data);
00256 }
00257
00258 static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
00259 {
00260 MpegTSWrite *ts = s->priv_data;
00261 uint8_t data[1012], *q, *desc_length_ptr, *program_info_length_ptr;
00262 int val, stream_type, i;
00263
00264 q = data;
00265 put16(&q, 0xe000 | service->pcr_pid);
00266
00267 program_info_length_ptr = q;
00268 q += 2;
00269
00270
00271
00272 val = 0xf000 | (q - program_info_length_ptr - 2);
00273 program_info_length_ptr[0] = val >> 8;
00274 program_info_length_ptr[1] = val;
00275
00276 for(i = 0; i < s->nb_streams; i++) {
00277 AVStream *st = s->streams[i];
00278 MpegTSWriteStream *ts_st = st->priv_data;
00279 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
00280 switch(st->codec->codec_id) {
00281 case AV_CODEC_ID_MPEG1VIDEO:
00282 case AV_CODEC_ID_MPEG2VIDEO:
00283 stream_type = STREAM_TYPE_VIDEO_MPEG2;
00284 break;
00285 case AV_CODEC_ID_MPEG4:
00286 stream_type = STREAM_TYPE_VIDEO_MPEG4;
00287 break;
00288 case AV_CODEC_ID_H264:
00289 stream_type = STREAM_TYPE_VIDEO_H264;
00290 break;
00291 case AV_CODEC_ID_DIRAC:
00292 stream_type = STREAM_TYPE_VIDEO_DIRAC;
00293 break;
00294 case AV_CODEC_ID_MP2:
00295 case AV_CODEC_ID_MP3:
00296 stream_type = STREAM_TYPE_AUDIO_MPEG1;
00297 break;
00298 case AV_CODEC_ID_AAC:
00299 stream_type = (ts->flags & MPEGTS_FLAG_AAC_LATM) ? STREAM_TYPE_AUDIO_AAC_LATM : STREAM_TYPE_AUDIO_AAC;
00300 break;
00301 case AV_CODEC_ID_AAC_LATM:
00302 stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
00303 break;
00304 case AV_CODEC_ID_AC3:
00305 stream_type = STREAM_TYPE_AUDIO_AC3;
00306 break;
00307 default:
00308 stream_type = STREAM_TYPE_PRIVATE_DATA;
00309 break;
00310 }
00311 *q++ = stream_type;
00312 put16(&q, 0xe000 | ts_st->pid);
00313 desc_length_ptr = q;
00314 q += 2;
00315
00316
00317 switch(st->codec->codec_type) {
00318 case AVMEDIA_TYPE_AUDIO:
00319 if(st->codec->codec_id==AV_CODEC_ID_EAC3){
00320 *q++=0x7a;
00321 *q++=1;
00322 *q++=0;
00323 }
00324 if(st->codec->codec_id==AV_CODEC_ID_S302M){
00325 *q++ = 0x05;
00326 *q++ = 4;
00327 *q++ = 'B';
00328 *q++ = 'S';
00329 *q++ = 'S';
00330 *q++ = 'D';
00331 }
00332
00333 if (lang) {
00334 char *p;
00335 char *next = lang->value;
00336 uint8_t *len_ptr;
00337
00338 *q++ = 0x0a;
00339 len_ptr = q++;
00340 *len_ptr = 0;
00341
00342 for (p = lang->value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
00343 next = strchr(p, ',');
00344 if (strlen(p) != 3 && (!next || next != p + 3))
00345 continue;
00346
00347 *q++ = *p++;
00348 *q++ = *p++;
00349 *q++ = *p++;
00350
00351 if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
00352 *q++ = 0x01;
00353 else if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
00354 *q++ = 0x02;
00355 else if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
00356 *q++ = 0x03;
00357 else
00358 *q++ = 0;
00359
00360 *len_ptr += 4;
00361 }
00362
00363 if (*len_ptr == 0)
00364 q -= 2;
00365 }
00366 break;
00367 case AVMEDIA_TYPE_SUBTITLE:
00368 {
00369 const char *language;
00370 language = lang && strlen(lang->value)==3 ? lang->value : "eng";
00371 *q++ = 0x59;
00372 *q++ = 8;
00373 *q++ = language[0];
00374 *q++ = language[1];
00375 *q++ = language[2];
00376 *q++ = 0x10;
00377 if(st->codec->extradata_size == 4) {
00378 memcpy(q, st->codec->extradata, 4);
00379 q += 4;
00380 } else {
00381 put16(&q, 1);
00382 put16(&q, 1);
00383 }
00384 }
00385 break;
00386 case AVMEDIA_TYPE_VIDEO:
00387 if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
00388 *q++ = 0x05;
00389 *q++ = 4;
00390 *q++ = 'd';
00391 *q++ = 'r';
00392 *q++ = 'a';
00393 *q++ = 'c';
00394 }
00395 break;
00396 }
00397
00398 val = 0xf000 | (q - desc_length_ptr - 2);
00399 desc_length_ptr[0] = val >> 8;
00400 desc_length_ptr[1] = val;
00401 }
00402 mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
00403 data, q - data);
00404 }
00405
00406
00407 static void putstr8(uint8_t **q_ptr, const char *str)
00408 {
00409 uint8_t *q;
00410 int len;
00411
00412 q = *q_ptr;
00413 if (!str)
00414 len = 0;
00415 else
00416 len = strlen(str);
00417 *q++ = len;
00418 memcpy(q, str, len);
00419 q += len;
00420 *q_ptr = q;
00421 }
00422
00423 static void mpegts_write_sdt(AVFormatContext *s)
00424 {
00425 MpegTSWrite *ts = s->priv_data;
00426 MpegTSService *service;
00427 uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
00428 int i, running_status, free_ca_mode, val;
00429
00430 q = data;
00431 put16(&q, ts->onid);
00432 *q++ = 0xff;
00433 for(i = 0; i < ts->nb_services; i++) {
00434 service = ts->services[i];
00435 put16(&q, service->sid);
00436 *q++ = 0xfc | 0x00;
00437 desc_list_len_ptr = q;
00438 q += 2;
00439 running_status = 4;
00440 free_ca_mode = 0;
00441
00442
00443 *q++ = 0x48;
00444 desc_len_ptr = q;
00445 q++;
00446 *q++ = 0x01;
00447 putstr8(&q, service->provider_name);
00448 putstr8(&q, service->name);
00449 desc_len_ptr[0] = q - desc_len_ptr - 1;
00450
00451
00452 val = (running_status << 13) | (free_ca_mode << 12) |
00453 (q - desc_list_len_ptr - 2);
00454 desc_list_len_ptr[0] = val >> 8;
00455 desc_list_len_ptr[1] = val;
00456 }
00457 mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, 0, 0, 0,
00458 data, q - data);
00459 }
00460
00461 static MpegTSService *mpegts_add_service(MpegTSWrite *ts,
00462 int sid,
00463 const char *provider_name,
00464 const char *name)
00465 {
00466 MpegTSService *service;
00467
00468 service = av_mallocz(sizeof(MpegTSService));
00469 if (!service)
00470 return NULL;
00471 service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
00472 service->sid = sid;
00473 service->provider_name = av_strdup(provider_name);
00474 service->name = av_strdup(name);
00475 service->pcr_pid = 0x1fff;
00476 dynarray_add(&ts->services, &ts->nb_services, service);
00477 return service;
00478 }
00479
00480 static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
00481 {
00482 return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
00483 ts->first_pcr;
00484 }
00485
00486 static void mpegts_prefix_m2ts_header(AVFormatContext *s)
00487 {
00488 MpegTSWrite *ts = s->priv_data;
00489 if (ts->m2ts_mode) {
00490 int64_t pcr = get_pcr(s->priv_data, s->pb);
00491 uint32_t tp_extra_header = pcr % 0x3fffffff;
00492 tp_extra_header = AV_RB32(&tp_extra_header);
00493 avio_write(s->pb, (unsigned char *) &tp_extra_header,
00494 sizeof(tp_extra_header));
00495 }
00496 }
00497
00498 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
00499 {
00500 AVFormatContext *ctx = s->opaque;
00501 mpegts_prefix_m2ts_header(ctx);
00502 avio_write(ctx->pb, packet, TS_PACKET_SIZE);
00503 }
00504
00505 static int mpegts_write_header(AVFormatContext *s)
00506 {
00507 MpegTSWrite *ts = s->priv_data;
00508 MpegTSWriteStream *ts_st;
00509 MpegTSService *service;
00510 AVStream *st, *pcr_st = NULL;
00511 AVDictionaryEntry *title, *provider;
00512 int i, j;
00513 const char *service_name;
00514 const char *provider_name;
00515 int *pids;
00516 int ret;
00517
00518 if (s->max_delay < 0)
00519 s->max_delay = 0;
00520
00521
00522 ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
00523
00524 ts->tsid = ts->transport_stream_id;
00525 ts->onid = ts->original_network_id;
00526
00527 title = av_dict_get(s->metadata, "service_name", NULL, 0);
00528 if (!title)
00529 title = av_dict_get(s->metadata, "title", NULL, 0);
00530 service_name = title ? title->value : DEFAULT_SERVICE_NAME;
00531 provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
00532 provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
00533 service = mpegts_add_service(ts, ts->service_id, provider_name, service_name);
00534 service->pmt.write_packet = section_write_packet;
00535 service->pmt.opaque = s;
00536 service->pmt.cc = 15;
00537
00538 ts->pat.pid = PAT_PID;
00539 ts->pat.cc = 15;
00540 ts->pat.write_packet = section_write_packet;
00541 ts->pat.opaque = s;
00542
00543 ts->sdt.pid = SDT_PID;
00544 ts->sdt.cc = 15;
00545 ts->sdt.write_packet = section_write_packet;
00546 ts->sdt.opaque = s;
00547
00548 pids = av_malloc(s->nb_streams * sizeof(*pids));
00549 if (!pids)
00550 return AVERROR(ENOMEM);
00551
00552
00553 for(i = 0;i < s->nb_streams; i++) {
00554 st = s->streams[i];
00555 avpriv_set_pts_info(st, 33, 1, 90000);
00556 ts_st = av_mallocz(sizeof(MpegTSWriteStream));
00557 if (!ts_st) {
00558 ret = AVERROR(ENOMEM);
00559 goto fail;
00560 }
00561 st->priv_data = ts_st;
00562 ts_st->payload = av_mallocz(ts->pes_payload_size);
00563 if (!ts_st->payload) {
00564 ret = AVERROR(ENOMEM);
00565 goto fail;
00566 }
00567 ts_st->service = service;
00568
00569
00570 if (st->id < 16) {
00571 ts_st->pid = ts->start_pid + i;
00572 } else if (st->id < 0x1FFF) {
00573 ts_st->pid = st->id;
00574 } else {
00575 av_log(s, AV_LOG_ERROR, "Invalid stream id %d, must be less than 8191\n", st->id);
00576 ret = AVERROR(EINVAL);
00577 goto fail;
00578 }
00579 if (ts_st->pid == service->pmt.pid) {
00580 av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
00581 ret = AVERROR(EINVAL);
00582 goto fail;
00583 }
00584 for (j = 0; j < i; j++)
00585 if (pids[j] == ts_st->pid) {
00586 av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
00587 ret = AVERROR(EINVAL);
00588 goto fail;
00589 }
00590 pids[i] = ts_st->pid;
00591 ts_st->payload_pts = AV_NOPTS_VALUE;
00592 ts_st->payload_dts = AV_NOPTS_VALUE;
00593 ts_st->first_pts_check = 1;
00594 ts_st->cc = 15;
00595
00596 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
00597 service->pcr_pid == 0x1fff) {
00598 service->pcr_pid = ts_st->pid;
00599 pcr_st = st;
00600 }
00601 if (st->codec->codec_id == AV_CODEC_ID_AAC &&
00602 st->codec->extradata_size > 0)
00603 {
00604 AVStream *ast;
00605 ts_st->amux = avformat_alloc_context();
00606 if (!ts_st->amux) {
00607 ret = AVERROR(ENOMEM);
00608 goto fail;
00609 }
00610 ts_st->amux->oformat = av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts", NULL, NULL);
00611 if (!ts_st->amux->oformat) {
00612 ret = AVERROR(EINVAL);
00613 goto fail;
00614 }
00615 ast = avformat_new_stream(ts_st->amux, NULL);
00616 ret = avcodec_copy_context(ast->codec, st->codec);
00617 if (ret != 0)
00618 goto fail;
00619 ret = avformat_write_header(ts_st->amux, NULL);
00620 if (ret < 0)
00621 goto fail;
00622 }
00623 }
00624
00625 av_free(pids);
00626
00627
00628 if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
00629 pcr_st = s->streams[0];
00630 ts_st = pcr_st->priv_data;
00631 service->pcr_pid = ts_st->pid;
00632 }
00633
00634 if (ts->mux_rate > 1) {
00635 service->pcr_packet_period = (ts->mux_rate * PCR_RETRANS_TIME) /
00636 (TS_PACKET_SIZE * 8 * 1000);
00637 ts->sdt_packet_period = (ts->mux_rate * SDT_RETRANS_TIME) /
00638 (TS_PACKET_SIZE * 8 * 1000);
00639 ts->pat_packet_period = (ts->mux_rate * PAT_RETRANS_TIME) /
00640 (TS_PACKET_SIZE * 8 * 1000);
00641
00642 if(ts->copyts < 1)
00643 ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
00644 } else {
00645
00646 ts->sdt_packet_period = 200;
00647 ts->pat_packet_period = 40;
00648 if (pcr_st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00649 if (!pcr_st->codec->frame_size) {
00650 av_log(s, AV_LOG_WARNING, "frame size not set\n");
00651 service->pcr_packet_period =
00652 pcr_st->codec->sample_rate/(10*512);
00653 } else {
00654 service->pcr_packet_period =
00655 pcr_st->codec->sample_rate/(10*pcr_st->codec->frame_size);
00656 }
00657 } else {
00658
00659 service->pcr_packet_period =
00660 pcr_st->codec->time_base.den/(10*pcr_st->codec->time_base.num);
00661 }
00662 if(!service->pcr_packet_period)
00663 service->pcr_packet_period = 1;
00664 }
00665
00666
00667 service->pcr_packet_count = service->pcr_packet_period;
00668 ts->pat_packet_count = ts->pat_packet_period-1;
00669 ts->sdt_packet_count = ts->sdt_packet_period-1;
00670
00671 if (ts->mux_rate == 1)
00672 av_log(s, AV_LOG_INFO, "muxrate VBR, ");
00673 else
00674 av_log(s, AV_LOG_INFO, "muxrate %d, ", ts->mux_rate);
00675 av_log(s, AV_LOG_INFO, "pcr every %d pkts, "
00676 "sdt every %d, pat/pmt every %d pkts\n",
00677 service->pcr_packet_period,
00678 ts->sdt_packet_period, ts->pat_packet_period);
00679
00680 if (ts->m2ts_mode == -1) {
00681 if (av_match_ext(s->filename, "m2ts")) {
00682 ts->m2ts_mode = 1;
00683 } else {
00684 ts->m2ts_mode = 0;
00685 }
00686 }
00687
00688 avio_flush(s->pb);
00689
00690 return 0;
00691
00692 fail:
00693 av_free(pids);
00694 for(i = 0;i < s->nb_streams; i++) {
00695 MpegTSWriteStream *ts_st;
00696 st = s->streams[i];
00697 ts_st = st->priv_data;
00698 if (ts_st) {
00699 av_freep(&ts_st->payload);
00700 if (ts_st->amux) {
00701 avformat_free_context(ts_st->amux);
00702 ts_st->amux = NULL;
00703 }
00704 }
00705 av_freep(&st->priv_data);
00706 }
00707 return ret;
00708 }
00709
00710
00711 static void retransmit_si_info(AVFormatContext *s, int force_pat)
00712 {
00713 MpegTSWrite *ts = s->priv_data;
00714 int i;
00715
00716 if (++ts->sdt_packet_count == ts->sdt_packet_period) {
00717 ts->sdt_packet_count = 0;
00718 mpegts_write_sdt(s);
00719 }
00720 if (++ts->pat_packet_count == ts->pat_packet_period || force_pat) {
00721 ts->pat_packet_count = 0;
00722 mpegts_write_pat(s);
00723 for(i = 0; i < ts->nb_services; i++) {
00724 mpegts_write_pmt(s, ts->services[i]);
00725 }
00726 }
00727 }
00728
00729 static int write_pcr_bits(uint8_t *buf, int64_t pcr)
00730 {
00731 int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
00732
00733 *buf++ = pcr_high >> 25;
00734 *buf++ = pcr_high >> 17;
00735 *buf++ = pcr_high >> 9;
00736 *buf++ = pcr_high >> 1;
00737 *buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
00738 *buf++ = pcr_low;
00739
00740 return 6;
00741 }
00742
00743
00744 static void mpegts_insert_null_packet(AVFormatContext *s)
00745 {
00746 uint8_t *q;
00747 uint8_t buf[TS_PACKET_SIZE];
00748
00749 q = buf;
00750 *q++ = 0x47;
00751 *q++ = 0x00 | 0x1f;
00752 *q++ = 0xff;
00753 *q++ = 0x10;
00754 memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
00755 mpegts_prefix_m2ts_header(s);
00756 avio_write(s->pb, buf, TS_PACKET_SIZE);
00757 }
00758
00759
00760 static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
00761 {
00762 MpegTSWrite *ts = s->priv_data;
00763 MpegTSWriteStream *ts_st = st->priv_data;
00764 uint8_t *q;
00765 uint8_t buf[TS_PACKET_SIZE];
00766
00767 q = buf;
00768 *q++ = 0x47;
00769 *q++ = ts_st->pid >> 8;
00770 *q++ = ts_st->pid;
00771 *q++ = 0x20 | ts_st->cc;
00772
00773 *q++ = TS_PACKET_SIZE - 5;
00774 *q++ = 0x10;
00775
00776
00777 q += write_pcr_bits(q, get_pcr(ts, s->pb));
00778
00779
00780 memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
00781 mpegts_prefix_m2ts_header(s);
00782 avio_write(s->pb, buf, TS_PACKET_SIZE);
00783 }
00784
00785 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
00786 {
00787 int val;
00788
00789 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
00790 *q++ = val;
00791 val = (((pts >> 15) & 0x7fff) << 1) | 1;
00792 *q++ = val >> 8;
00793 *q++ = val;
00794 val = (((pts) & 0x7fff) << 1) | 1;
00795 *q++ = val >> 8;
00796 *q++ = val;
00797 }
00798
00799
00800 static void set_af_flag(uint8_t *pkt, int flag)
00801 {
00802
00803 av_assert0(flag);
00804
00805 if ((pkt[3] & 0x20) == 0) {
00806
00807 pkt[3] |= 0x20;
00808
00809 pkt[4] = 1;
00810 pkt[5] = 0;
00811 }
00812 pkt[5] |= flag;
00813 }
00814
00815
00816 static void extend_af(uint8_t *pkt, int size)
00817 {
00818
00819 av_assert0(pkt[3] & 0x20);
00820 pkt[4] += size;
00821 }
00822
00823
00824 static uint8_t *get_ts_payload_start(uint8_t *pkt)
00825 {
00826 if (pkt[3] & 0x20)
00827 return pkt + 5 + pkt[4];
00828 else
00829 return pkt + 4;
00830 }
00831
00832
00833
00834
00835
00836
00837 static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
00838 const uint8_t *payload, int payload_size,
00839 int64_t pts, int64_t dts, int key)
00840 {
00841 MpegTSWriteStream *ts_st = st->priv_data;
00842 MpegTSWrite *ts = s->priv_data;
00843 uint8_t buf[TS_PACKET_SIZE];
00844 uint8_t *q;
00845 int val, is_start, len, header_len, write_pcr, private_code, flags;
00846 int afc_len, stuffing_len;
00847 int64_t pcr = -1;
00848 int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
00849 int force_pat = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
00850
00851 is_start = 1;
00852 while (payload_size > 0) {
00853 retransmit_si_info(s, force_pat);
00854 force_pat = 0;
00855
00856 write_pcr = 0;
00857 if (ts_st->pid == ts_st->service->pcr_pid) {
00858 if (ts->mux_rate > 1 || is_start)
00859 ts_st->service->pcr_packet_count++;
00860 if (ts_st->service->pcr_packet_count >=
00861 ts_st->service->pcr_packet_period) {
00862 ts_st->service->pcr_packet_count = 0;
00863 write_pcr = 1;
00864 }
00865 }
00866
00867 if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
00868 (dts - get_pcr(ts, s->pb)/300) > delay) {
00869
00870 if (write_pcr)
00871 mpegts_insert_pcr_only(s, st);
00872 else
00873 mpegts_insert_null_packet(s);
00874 continue;
00875 }
00876
00877
00878 q = buf;
00879 *q++ = 0x47;
00880 val = (ts_st->pid >> 8);
00881 if (is_start)
00882 val |= 0x40;
00883 *q++ = val;
00884 *q++ = ts_st->pid;
00885 ts_st->cc = (ts_st->cc + 1) & 0xf;
00886 *q++ = 0x10 | ts_st->cc;
00887 if (key && is_start && pts != AV_NOPTS_VALUE) {
00888
00889 if (ts_st->pid == ts_st->service->pcr_pid)
00890 write_pcr = 1;
00891 set_af_flag(buf, 0x40);
00892 q = get_ts_payload_start(buf);
00893 }
00894 if (write_pcr) {
00895 set_af_flag(buf, 0x10);
00896 q = get_ts_payload_start(buf);
00897
00898 if (ts->mux_rate > 1)
00899 pcr = get_pcr(ts, s->pb);
00900 else
00901 pcr = (dts - delay)*300;
00902 if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
00903 av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
00904 extend_af(buf, write_pcr_bits(q, pcr));
00905 q = get_ts_payload_start(buf);
00906 }
00907 if (is_start) {
00908 int pes_extension = 0;
00909
00910 *q++ = 0x00;
00911 *q++ = 0x00;
00912 *q++ = 0x01;
00913 private_code = 0;
00914 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00915 if (st->codec->codec_id == AV_CODEC_ID_DIRAC) {
00916 *q++ = 0xfd;
00917 } else
00918 *q++ = 0xe0;
00919 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
00920 (st->codec->codec_id == AV_CODEC_ID_MP2 ||
00921 st->codec->codec_id == AV_CODEC_ID_MP3 ||
00922 st->codec->codec_id == AV_CODEC_ID_AAC)) {
00923 *q++ = 0xc0;
00924 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
00925 st->codec->codec_id == AV_CODEC_ID_AC3 &&
00926 ts->m2ts_mode) {
00927 *q++ = 0xfd;
00928 } else {
00929 *q++ = 0xbd;
00930 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
00931 private_code = 0x20;
00932 }
00933 }
00934 header_len = 0;
00935 flags = 0;
00936 if (pts != AV_NOPTS_VALUE) {
00937 header_len += 5;
00938 flags |= 0x80;
00939 }
00940 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
00941 header_len += 5;
00942 flags |= 0x40;
00943 }
00944 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
00945 st->codec->codec_id == AV_CODEC_ID_DIRAC) {
00946
00947 pes_extension = 1;
00948 flags |= 0x01;
00949
00950
00951
00952
00953
00954
00955 header_len += 3;
00956 }
00957
00958
00959
00960 if (ts->m2ts_mode &&
00961 st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
00962 st->codec->codec_id == AV_CODEC_ID_AC3) {
00963
00964 pes_extension = 1;
00965 flags |= 0x01;
00966 header_len += 3;
00967 }
00968 len = payload_size + header_len + 3;
00969 if (private_code != 0)
00970 len++;
00971 if (len > 0xffff)
00972 len = 0;
00973 *q++ = len >> 8;
00974 *q++ = len;
00975 val = 0x80;
00976
00977 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
00978 val |= 0x04;
00979 *q++ = val;
00980 *q++ = flags;
00981 *q++ = header_len;
00982 if (pts != AV_NOPTS_VALUE) {
00983 write_pts(q, flags >> 6, pts);
00984 q += 5;
00985 }
00986 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
00987 write_pts(q, 1, dts);
00988 q += 5;
00989 }
00990 if (pes_extension && st->codec->codec_id == AV_CODEC_ID_DIRAC) {
00991 flags = 0x01;
00992 *q++ = flags;
00993 *q++ = 0x80 | 0x01;
00994
00995
00996
00997
00998 *q++ = 0x00 | 0x60;
00999 }
01000
01001 if (ts->m2ts_mode &&
01002 pes_extension &&
01003 st->codec->codec_id == AV_CODEC_ID_AC3) {
01004 flags = 0x01;
01005 *q++ = flags;
01006 *q++ = 0x80 | 0x01;
01007 *q++ = 0x00 | 0x71;
01008 }
01009
01010
01011 if (private_code != 0)
01012 *q++ = private_code;
01013 is_start = 0;
01014 }
01015
01016 header_len = q - buf;
01017
01018 len = TS_PACKET_SIZE - header_len;
01019 if (len > payload_size)
01020 len = payload_size;
01021 stuffing_len = TS_PACKET_SIZE - header_len - len;
01022 if (stuffing_len > 0) {
01023
01024 if (buf[3] & 0x20) {
01025
01026 afc_len = buf[4] + 1;
01027 memmove(buf + 4 + afc_len + stuffing_len,
01028 buf + 4 + afc_len,
01029 header_len - (4 + afc_len));
01030 buf[4] += stuffing_len;
01031 memset(buf + 4 + afc_len, 0xff, stuffing_len);
01032 } else {
01033
01034 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
01035 buf[3] |= 0x20;
01036 buf[4] = stuffing_len - 1;
01037 if (stuffing_len >= 2) {
01038 buf[5] = 0x00;
01039 memset(buf + 6, 0xff, stuffing_len - 2);
01040 }
01041 }
01042 }
01043 memcpy(buf + TS_PACKET_SIZE - len, payload, len);
01044 payload += len;
01045 payload_size -= len;
01046 mpegts_prefix_m2ts_header(s);
01047 avio_write(s->pb, buf, TS_PACKET_SIZE);
01048 }
01049 avio_flush(s->pb);
01050 ts_st->prev_payload_key = key;
01051 }
01052
01053 static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
01054 {
01055 AVStream *st = s->streams[pkt->stream_index];
01056 int size = pkt->size;
01057 uint8_t *buf= pkt->data;
01058 uint8_t *data= NULL;
01059 MpegTSWrite *ts = s->priv_data;
01060 MpegTSWriteStream *ts_st = st->priv_data;
01061 const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE)*2;
01062 int64_t dts = pkt->dts, pts = pkt->pts;
01063
01064 if (ts->reemit_pat_pmt) {
01065 av_log(s, AV_LOG_WARNING, "resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
01066 ts->reemit_pat_pmt = 0;
01067 ts->flags |= MPEGTS_FLAG_REEMIT_PAT_PMT;
01068 }
01069
01070 if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
01071 ts->pat_packet_count = ts->pat_packet_period - 1;
01072 ts->sdt_packet_count = ts->sdt_packet_period - 1;
01073 ts->flags &= ~MPEGTS_FLAG_REEMIT_PAT_PMT;
01074 }
01075
01076 if(ts->copyts < 1){
01077 if (pts != AV_NOPTS_VALUE)
01078 pts += delay;
01079 if (dts != AV_NOPTS_VALUE)
01080 dts += delay;
01081 }
01082
01083 if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
01084 av_log(s, AV_LOG_ERROR, "first pts value must set\n");
01085 return AVERROR_INVALIDDATA;
01086 }
01087 ts_st->first_pts_check = 0;
01088
01089 if (st->codec->codec_id == AV_CODEC_ID_H264) {
01090 const uint8_t *p = buf, *buf_end = p+size;
01091 uint32_t state = -1;
01092
01093 if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
01094 av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
01095 "no startcode found, use the h264_mp4toannexb bitstream filter (-bsf h264_mp4toannexb)\n");
01096 return AVERROR(EINVAL);
01097 }
01098
01099 do {
01100 p = avpriv_mpv_find_start_code(p, buf_end, &state);
01101
01102 } while (p < buf_end && (state & 0x1f) != 9 &&
01103 (state & 0x1f) != 5 && (state & 0x1f) != 1);
01104
01105 if ((state & 0x1f) != 9) {
01106 data = av_malloc(pkt->size+6);
01107 if (!data)
01108 return AVERROR(ENOMEM);
01109 memcpy(data+6, pkt->data, pkt->size);
01110 AV_WB32(data, 0x00000001);
01111 data[4] = 0x09;
01112 data[5] = 0xf0;
01113 buf = data;
01114 size = pkt->size+6;
01115 }
01116 } else if (st->codec->codec_id == AV_CODEC_ID_AAC) {
01117 if (pkt->size < 2) {
01118 av_log(s, AV_LOG_ERROR, "AAC packet too short\n");
01119 return AVERROR_INVALIDDATA;
01120 }
01121 if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
01122 int ret;
01123 AVPacket pkt2;
01124
01125 if (!ts_st->amux) {
01126 av_log(s, AV_LOG_ERROR, "AAC bitstream not in ADTS format "
01127 "and extradata missing\n");
01128 return AVERROR_INVALIDDATA;
01129 }
01130
01131 av_init_packet(&pkt2);
01132 pkt2.data = pkt->data;
01133 pkt2.size = pkt->size;
01134 ret = avio_open_dyn_buf(&ts_st->amux->pb);
01135 if (ret < 0)
01136 return AVERROR(ENOMEM);
01137
01138 ret = av_write_frame(ts_st->amux, &pkt2);
01139 if (ret < 0) {
01140 avio_close_dyn_buf(ts_st->amux->pb, &data);
01141 ts_st->amux->pb = NULL;
01142 av_free(data);
01143 return ret;
01144 }
01145 size = avio_close_dyn_buf(ts_st->amux->pb, &data);
01146 ts_st->amux->pb = NULL;
01147 buf = data;
01148 }
01149 }
01150
01151 if (pkt->dts != AV_NOPTS_VALUE) {
01152 int i;
01153 for(i=0; i<s->nb_streams; i++){
01154 AVStream *st2 = s->streams[i];
01155 MpegTSWriteStream *ts_st2 = st2->priv_data;
01156 if( ts_st2->payload_size
01157 && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)){
01158 mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
01159 ts_st2->payload_pts, ts_st2->payload_dts,
01160 ts_st2->payload_flags & AV_PKT_FLAG_KEY);
01161 ts_st2->payload_size = 0;
01162 }
01163 }
01164 }
01165
01166 if (ts_st->payload_size && ts_st->payload_size + size > ts->pes_payload_size) {
01167 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
01168 ts_st->payload_pts, ts_st->payload_dts,
01169 ts_st->payload_flags & AV_PKT_FLAG_KEY);
01170 ts_st->payload_size = 0;
01171 }
01172
01173 if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO || size > ts->pes_payload_size) {
01174 av_assert0(!ts_st->payload_size);
01175
01176 mpegts_write_pes(s, st, buf, size, pts, dts, pkt->flags & AV_PKT_FLAG_KEY);
01177 av_free(data);
01178 return 0;
01179 }
01180
01181 if (!ts_st->payload_size) {
01182 ts_st->payload_pts = pts;
01183 ts_st->payload_dts = dts;
01184 ts_st->payload_flags = pkt->flags;
01185 }
01186
01187 memcpy(ts_st->payload + ts_st->payload_size, buf, size);
01188 ts_st->payload_size += size;
01189
01190 av_free(data);
01191
01192 return 0;
01193 }
01194
01195 static void mpegts_write_flush(AVFormatContext *s)
01196 {
01197 int i;
01198
01199
01200 for(i = 0; i < s->nb_streams; i++) {
01201 AVStream *st = s->streams[i];
01202 MpegTSWriteStream *ts_st = st->priv_data;
01203 if (ts_st->payload_size > 0) {
01204 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
01205 ts_st->payload_pts, ts_st->payload_dts,
01206 ts_st->payload_flags & AV_PKT_FLAG_KEY);
01207 ts_st->payload_size = 0;
01208 }
01209 }
01210 avio_flush(s->pb);
01211 }
01212
01213 static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
01214 {
01215 if (!pkt) {
01216 mpegts_write_flush(s);
01217 return 1;
01218 } else {
01219 return mpegts_write_packet_internal(s, pkt);
01220 }
01221 }
01222
01223 static int mpegts_write_end(AVFormatContext *s)
01224 {
01225 MpegTSWrite *ts = s->priv_data;
01226 MpegTSService *service;
01227 int i;
01228
01229 mpegts_write_flush(s);
01230
01231 for(i = 0; i < s->nb_streams; i++) {
01232 AVStream *st = s->streams[i];
01233 MpegTSWriteStream *ts_st = st->priv_data;
01234 av_freep(&ts_st->payload);
01235 if (ts_st->amux) {
01236 avformat_free_context(ts_st->amux);
01237 ts_st->amux = NULL;
01238 }
01239 }
01240
01241 for(i = 0; i < ts->nb_services; i++) {
01242 service = ts->services[i];
01243 av_freep(&service->provider_name);
01244 av_freep(&service->name);
01245 av_free(service);
01246 }
01247 av_free(ts->services);
01248
01249 return 0;
01250 }
01251
01252 AVOutputFormat ff_mpegts_muxer = {
01253 .name = "mpegts",
01254 .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
01255 .mime_type = "video/x-mpegts",
01256 .extensions = "ts,m2t,m2ts,mts",
01257 .priv_data_size = sizeof(MpegTSWrite),
01258 .audio_codec = AV_CODEC_ID_MP2,
01259 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
01260 .write_header = mpegts_write_header,
01261 .write_packet = mpegts_write_packet,
01262 .write_trailer = mpegts_write_end,
01263 .flags = AVFMT_ALLOW_FLUSH,
01264 .priv_class = &mpegts_muxer_class,
01265 };