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