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 "libavcodec/mpegvideo.h"
00025 #include "avformat.h"
00026 #include "internal.h"
00027 #include "mpegts.h"
00028 #include "adts.h"
00029
00030
00031
00032
00033
00034
00035 typedef struct MpegTSSection {
00036 int pid;
00037 int cc;
00038 void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
00039 void *opaque;
00040 } MpegTSSection;
00041
00042 typedef struct MpegTSService {
00043 MpegTSSection pmt;
00044 int sid;
00045 char *name;
00046 char *provider_name;
00047 int pcr_pid;
00048 int pcr_packet_count;
00049 int pcr_packet_period;
00050 } MpegTSService;
00051
00052 typedef struct MpegTSWrite {
00053 MpegTSSection pat;
00054 MpegTSSection sdt;
00055 MpegTSService **services;
00056 int sdt_packet_count;
00057 int sdt_packet_period;
00058 int pat_packet_count;
00059 int pat_packet_period;
00060 int nb_services;
00061 int onid;
00062 int tsid;
00063 uint64_t cur_pcr;
00064 int mux_rate;
00065 } MpegTSWrite;
00066
00067
00068 static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
00069 {
00070 MpegTSWrite *ts = ((AVFormatContext*)s->opaque)->priv_data;
00071 unsigned int crc;
00072 unsigned char packet[TS_PACKET_SIZE];
00073 const unsigned char *buf_ptr;
00074 unsigned char *q;
00075 int first, b, len1, left;
00076
00077 crc = bswap_32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, buf, len - 4));
00078 buf[len - 4] = (crc >> 24) & 0xff;
00079 buf[len - 3] = (crc >> 16) & 0xff;
00080 buf[len - 2] = (crc >> 8) & 0xff;
00081 buf[len - 1] = (crc) & 0xff;
00082
00083
00084 buf_ptr = buf;
00085 while (len > 0) {
00086 first = (buf == buf_ptr);
00087 q = packet;
00088 *q++ = 0x47;
00089 b = (s->pid >> 8);
00090 if (first)
00091 b |= 0x40;
00092 *q++ = b;
00093 *q++ = s->pid;
00094 s->cc = (s->cc + 1) & 0xf;
00095 *q++ = 0x10 | s->cc;
00096 if (first)
00097 *q++ = 0;
00098 len1 = TS_PACKET_SIZE - (q - packet);
00099 if (len1 > len)
00100 len1 = len;
00101 memcpy(q, buf_ptr, len1);
00102 q += len1;
00103
00104 left = TS_PACKET_SIZE - (q - packet);
00105 if (left > 0)
00106 memset(q, 0xff, left);
00107
00108 s->write_packet(s, packet);
00109
00110 buf_ptr += len1;
00111 len -= len1;
00112
00113 ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
00114 }
00115 }
00116
00117 static inline void put16(uint8_t **q_ptr, int val)
00118 {
00119 uint8_t *q;
00120 q = *q_ptr;
00121 *q++ = val >> 8;
00122 *q++ = val;
00123 *q_ptr = q;
00124 }
00125
00126 static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
00127 int version, int sec_num, int last_sec_num,
00128 uint8_t *buf, int len)
00129 {
00130 uint8_t section[1024], *q;
00131 unsigned int tot_len;
00132
00133 tot_len = 3 + 5 + len + 4;
00134
00135 if (tot_len > 1024)
00136 return -1;
00137
00138 q = section;
00139 *q++ = tid;
00140 put16(&q, 0xb000 | (len + 5 + 4));
00141 put16(&q, id);
00142 *q++ = 0xc1 | (version << 1);
00143 *q++ = sec_num;
00144 *q++ = last_sec_num;
00145 memcpy(q, buf, len);
00146
00147 mpegts_write_section(s, section, tot_len);
00148 return 0;
00149 }
00150
00151
00152
00153
00154 #define DEFAULT_PMT_START_PID 0x1000
00155 #define DEFAULT_START_PID 0x0100
00156 #define DEFAULT_PROVIDER_NAME "FFmpeg"
00157 #define DEFAULT_SERVICE_NAME "Service01"
00158
00159
00160 #define DEFAULT_ONID 0x0001
00161 #define DEFAULT_TSID 0x0001
00162 #define DEFAULT_SID 0x0001
00163
00164
00165 #define DEFAULT_PES_HEADER_FREQ 16
00166 #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
00167
00168
00169 #define SDT_RETRANS_TIME 500
00170 #define PAT_RETRANS_TIME 100
00171 #define PCR_RETRANS_TIME 20
00172
00173 typedef struct MpegTSWriteStream {
00174 struct MpegTSService *service;
00175 int pid;
00176 int cc;
00177 int payload_index;
00178 int first_pts_check;
00179 int64_t payload_pts;
00180 int64_t payload_dts;
00181 uint8_t payload[DEFAULT_PES_PAYLOAD_SIZE];
00182 ADTSContext *adts;
00183 } MpegTSWriteStream;
00184
00185 static void mpegts_write_pat(AVFormatContext *s)
00186 {
00187 MpegTSWrite *ts = s->priv_data;
00188 MpegTSService *service;
00189 uint8_t data[1012], *q;
00190 int i;
00191
00192 q = data;
00193 for(i = 0; i < ts->nb_services; i++) {
00194 service = ts->services[i];
00195 put16(&q, service->sid);
00196 put16(&q, 0xe000 | service->pmt.pid);
00197 }
00198 mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, 0, 0, 0,
00199 data, q - data);
00200 }
00201
00202 static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
00203 {
00204
00205 uint8_t data[1012], *q, *desc_length_ptr, *program_info_length_ptr;
00206 int val, stream_type, i;
00207
00208 q = data;
00209 put16(&q, 0xe000 | service->pcr_pid);
00210
00211 program_info_length_ptr = q;
00212 q += 2;
00213
00214
00215
00216 val = 0xf000 | (q - program_info_length_ptr - 2);
00217 program_info_length_ptr[0] = val >> 8;
00218 program_info_length_ptr[1] = val;
00219
00220 for(i = 0; i < s->nb_streams; i++) {
00221 AVStream *st = s->streams[i];
00222 MpegTSWriteStream *ts_st = st->priv_data;
00223 AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL,0);
00224 switch(st->codec->codec_id) {
00225 case CODEC_ID_MPEG1VIDEO:
00226 case CODEC_ID_MPEG2VIDEO:
00227 stream_type = STREAM_TYPE_VIDEO_MPEG2;
00228 break;
00229 case CODEC_ID_MPEG4:
00230 stream_type = STREAM_TYPE_VIDEO_MPEG4;
00231 break;
00232 case CODEC_ID_H264:
00233 stream_type = STREAM_TYPE_VIDEO_H264;
00234 break;
00235 case CODEC_ID_DIRAC:
00236 stream_type = STREAM_TYPE_VIDEO_DIRAC;
00237 break;
00238 case CODEC_ID_MP2:
00239 case CODEC_ID_MP3:
00240 stream_type = STREAM_TYPE_AUDIO_MPEG1;
00241 break;
00242 case CODEC_ID_AAC:
00243 stream_type = STREAM_TYPE_AUDIO_AAC;
00244 break;
00245 case CODEC_ID_AC3:
00246 stream_type = STREAM_TYPE_AUDIO_AC3;
00247 break;
00248 default:
00249 stream_type = STREAM_TYPE_PRIVATE_DATA;
00250 break;
00251 }
00252 *q++ = stream_type;
00253 put16(&q, 0xe000 | ts_st->pid);
00254 desc_length_ptr = q;
00255 q += 2;
00256
00257
00258 switch(st->codec->codec_type) {
00259 case AVMEDIA_TYPE_AUDIO:
00260 if (lang && strlen(lang->value) == 3) {
00261 *q++ = 0x0a;
00262 *q++ = 4;
00263 *q++ = lang->value[0];
00264 *q++ = lang->value[1];
00265 *q++ = lang->value[2];
00266 *q++ = 0;
00267 }
00268 break;
00269 case AVMEDIA_TYPE_SUBTITLE:
00270 {
00271 const char *language;
00272 language = lang && strlen(lang->value)==3 ? lang->value : "eng";
00273 *q++ = 0x59;
00274 *q++ = 8;
00275 *q++ = language[0];
00276 *q++ = language[1];
00277 *q++ = language[2];
00278 *q++ = 0x10;
00279 put16(&q, 1);
00280 put16(&q, 1);
00281 }
00282 break;
00283 case AVMEDIA_TYPE_VIDEO:
00284 if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
00285 *q++ = 0x05;
00286 *q++ = 4;
00287 *q++ = 'd';
00288 *q++ = 'r';
00289 *q++ = 'a';
00290 *q++ = 'c';
00291 }
00292 break;
00293 }
00294
00295 val = 0xf000 | (q - desc_length_ptr - 2);
00296 desc_length_ptr[0] = val >> 8;
00297 desc_length_ptr[1] = val;
00298 }
00299 mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
00300 data, q - data);
00301 }
00302
00303
00304 static void putstr8(uint8_t **q_ptr, const char *str)
00305 {
00306 uint8_t *q;
00307 int len;
00308
00309 q = *q_ptr;
00310 if (!str)
00311 len = 0;
00312 else
00313 len = strlen(str);
00314 *q++ = len;
00315 memcpy(q, str, len);
00316 q += len;
00317 *q_ptr = q;
00318 }
00319
00320 static void mpegts_write_sdt(AVFormatContext *s)
00321 {
00322 MpegTSWrite *ts = s->priv_data;
00323 MpegTSService *service;
00324 uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
00325 int i, running_status, free_ca_mode, val;
00326
00327 q = data;
00328 put16(&q, ts->onid);
00329 *q++ = 0xff;
00330 for(i = 0; i < ts->nb_services; i++) {
00331 service = ts->services[i];
00332 put16(&q, service->sid);
00333 *q++ = 0xfc | 0x00;
00334 desc_list_len_ptr = q;
00335 q += 2;
00336 running_status = 4;
00337 free_ca_mode = 0;
00338
00339
00340 *q++ = 0x48;
00341 desc_len_ptr = q;
00342 q++;
00343 *q++ = 0x01;
00344 putstr8(&q, service->provider_name);
00345 putstr8(&q, service->name);
00346 desc_len_ptr[0] = q - desc_len_ptr - 1;
00347
00348
00349 val = (running_status << 13) | (free_ca_mode << 12) |
00350 (q - desc_list_len_ptr - 2);
00351 desc_list_len_ptr[0] = val >> 8;
00352 desc_list_len_ptr[1] = val;
00353 }
00354 mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, 0, 0, 0,
00355 data, q - data);
00356 }
00357
00358 static MpegTSService *mpegts_add_service(MpegTSWrite *ts,
00359 int sid,
00360 const char *provider_name,
00361 const char *name)
00362 {
00363 MpegTSService *service;
00364
00365 service = av_mallocz(sizeof(MpegTSService));
00366 if (!service)
00367 return NULL;
00368 service->pmt.pid = DEFAULT_PMT_START_PID + ts->nb_services - 1;
00369 service->sid = sid;
00370 service->provider_name = av_strdup(provider_name);
00371 service->name = av_strdup(name);
00372 service->pcr_pid = 0x1fff;
00373 dynarray_add(&ts->services, &ts->nb_services, service);
00374 return service;
00375 }
00376
00377 static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
00378 {
00379 AVFormatContext *ctx = s->opaque;
00380 put_buffer(ctx->pb, packet, TS_PACKET_SIZE);
00381 }
00382
00383 static int mpegts_write_header(AVFormatContext *s)
00384 {
00385 MpegTSWrite *ts = s->priv_data;
00386 MpegTSWriteStream *ts_st;
00387 MpegTSService *service;
00388 AVStream *st, *pcr_st = NULL;
00389 AVMetadataTag *title;
00390 int i;
00391 const char *service_name;
00392
00393 ts->tsid = DEFAULT_TSID;
00394 ts->onid = DEFAULT_ONID;
00395
00396 title = av_metadata_get(s->metadata, "title", NULL, 0);
00397 service_name = title ? title->value : DEFAULT_SERVICE_NAME;
00398 service = mpegts_add_service(ts, DEFAULT_SID,
00399 DEFAULT_PROVIDER_NAME, service_name);
00400 service->pmt.write_packet = section_write_packet;
00401 service->pmt.opaque = s;
00402 service->pmt.cc = 15;
00403
00404 ts->pat.pid = PAT_PID;
00405 ts->pat.cc = 15;
00406 ts->pat.write_packet = section_write_packet;
00407 ts->pat.opaque = s;
00408
00409 ts->sdt.pid = SDT_PID;
00410 ts->sdt.cc = 15;
00411 ts->sdt.write_packet = section_write_packet;
00412 ts->sdt.opaque = s;
00413
00414
00415 for(i = 0;i < s->nb_streams; i++) {
00416 st = s->streams[i];
00417 ts_st = av_mallocz(sizeof(MpegTSWriteStream));
00418 if (!ts_st)
00419 goto fail;
00420 st->priv_data = ts_st;
00421 ts_st->service = service;
00422 ts_st->pid = DEFAULT_START_PID + i;
00423 ts_st->payload_pts = AV_NOPTS_VALUE;
00424 ts_st->payload_dts = AV_NOPTS_VALUE;
00425 ts_st->first_pts_check = 1;
00426 ts_st->cc = 15;
00427
00428 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
00429 service->pcr_pid == 0x1fff) {
00430 service->pcr_pid = ts_st->pid;
00431 pcr_st = st;
00432 }
00433 if (st->codec->codec_id == CODEC_ID_AAC &&
00434 st->codec->extradata_size > 0) {
00435 ts_st->adts = av_mallocz(sizeof(*ts_st->adts));
00436 if (!ts_st->adts)
00437 return AVERROR(ENOMEM);
00438 if (ff_adts_decode_extradata(s, ts_st->adts, st->codec->extradata,
00439 st->codec->extradata_size) < 0)
00440 return -1;
00441 }
00442 }
00443
00444
00445 if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
00446 pcr_st = s->streams[0];
00447 ts_st = pcr_st->priv_data;
00448 service->pcr_pid = ts_st->pid;
00449 }
00450
00451 ts->mux_rate = s->mux_rate ? s->mux_rate : 1;
00452
00453 if (ts->mux_rate > 1) {
00454 service->pcr_packet_period = (ts->mux_rate * PCR_RETRANS_TIME) /
00455 (TS_PACKET_SIZE * 8 * 1000);
00456 ts->sdt_packet_period = (ts->mux_rate * SDT_RETRANS_TIME) /
00457 (TS_PACKET_SIZE * 8 * 1000);
00458 ts->pat_packet_period = (ts->mux_rate * PAT_RETRANS_TIME) /
00459 (TS_PACKET_SIZE * 8 * 1000);
00460
00461 ts->cur_pcr = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
00462 } else {
00463
00464 ts->sdt_packet_period = 200;
00465 ts->pat_packet_period = 40;
00466 if (pcr_st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00467 if (!pcr_st->codec->frame_size) {
00468 av_log(s, AV_LOG_WARNING, "frame size not set\n");
00469 service->pcr_packet_period =
00470 pcr_st->codec->sample_rate/(10*512);
00471 } else {
00472 service->pcr_packet_period =
00473 pcr_st->codec->sample_rate/(10*pcr_st->codec->frame_size);
00474 }
00475 } else {
00476
00477 service->pcr_packet_period =
00478 pcr_st->codec->time_base.den/(10*pcr_st->codec->time_base.num);
00479 }
00480 }
00481
00482
00483 service->pcr_packet_count = service->pcr_packet_period;
00484 ts->pat_packet_count = ts->pat_packet_period-1;
00485 ts->sdt_packet_count = ts->sdt_packet_period-1;
00486
00487 av_log(s, AV_LOG_INFO,
00488 "muxrate %d bps, pcr every %d pkts, "
00489 "sdt every %d, pat/pmt every %d pkts\n",
00490 ts->mux_rate, service->pcr_packet_period,
00491 ts->sdt_packet_period, ts->pat_packet_period);
00492
00493
00494 put_flush_packet(s->pb);
00495
00496 return 0;
00497
00498 fail:
00499 for(i = 0;i < s->nb_streams; i++) {
00500 st = s->streams[i];
00501 av_free(st->priv_data);
00502 }
00503 return -1;
00504 }
00505
00506
00507 static void retransmit_si_info(AVFormatContext *s)
00508 {
00509 MpegTSWrite *ts = s->priv_data;
00510 int i;
00511
00512 if (++ts->sdt_packet_count == ts->sdt_packet_period) {
00513 ts->sdt_packet_count = 0;
00514 mpegts_write_sdt(s);
00515 }
00516 if (++ts->pat_packet_count == ts->pat_packet_period) {
00517 ts->pat_packet_count = 0;
00518 mpegts_write_pat(s);
00519 for(i = 0; i < ts->nb_services; i++) {
00520 mpegts_write_pmt(s, ts->services[i]);
00521 }
00522 }
00523 }
00524
00525
00526 static void mpegts_insert_null_packet(AVFormatContext *s)
00527 {
00528 MpegTSWrite *ts = s->priv_data;
00529 uint8_t *q;
00530 uint8_t buf[TS_PACKET_SIZE];
00531
00532 q = buf;
00533 *q++ = 0x47;
00534 *q++ = 0x00 | 0x1f;
00535 *q++ = 0xff;
00536 *q++ = 0x10;
00537 memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
00538 put_buffer(s->pb, buf, TS_PACKET_SIZE);
00539 ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
00540 }
00541
00542
00543 static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
00544 {
00545 MpegTSWrite *ts = s->priv_data;
00546 MpegTSWriteStream *ts_st = st->priv_data;
00547 uint8_t *q;
00548 uint64_t pcr = ts->cur_pcr;
00549 uint8_t buf[TS_PACKET_SIZE];
00550
00551 q = buf;
00552 *q++ = 0x47;
00553 *q++ = ts_st->pid >> 8;
00554 *q++ = ts_st->pid;
00555 *q++ = 0x20 | ts_st->cc;
00556
00557 *q++ = TS_PACKET_SIZE - 5;
00558 *q++ = 0x10;
00559
00560
00561 *q++ = pcr >> 25;
00562 *q++ = pcr >> 17;
00563 *q++ = pcr >> 9;
00564 *q++ = pcr >> 1;
00565 *q++ = (pcr & 1) << 7;
00566 *q++ = 0;
00567
00568
00569 memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
00570 put_buffer(s->pb, buf, TS_PACKET_SIZE);
00571 ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
00572 }
00573
00574 static void write_pts(uint8_t *q, int fourbits, int64_t pts)
00575 {
00576 int val;
00577
00578 val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
00579 *q++ = val;
00580 val = (((pts >> 15) & 0x7fff) << 1) | 1;
00581 *q++ = val >> 8;
00582 *q++ = val;
00583 val = (((pts) & 0x7fff) << 1) | 1;
00584 *q++ = val >> 8;
00585 *q++ = val;
00586 }
00587
00588
00589
00590
00591
00592
00593 static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
00594 const uint8_t *payload, int payload_size,
00595 int64_t pts, int64_t dts)
00596 {
00597 MpegTSWriteStream *ts_st = st->priv_data;
00598 MpegTSWrite *ts = s->priv_data;
00599 uint8_t buf[TS_PACKET_SIZE];
00600 uint8_t *q;
00601 int val, is_start, len, header_len, write_pcr, private_code, flags;
00602 int afc_len, stuffing_len;
00603 int64_t pcr = -1;
00604 int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
00605
00606 is_start = 1;
00607 while (payload_size > 0) {
00608 retransmit_si_info(s);
00609
00610 write_pcr = 0;
00611 if (ts_st->pid == ts_st->service->pcr_pid) {
00612 if (ts->mux_rate > 1 || is_start)
00613 ts_st->service->pcr_packet_count++;
00614 if (ts_st->service->pcr_packet_count >=
00615 ts_st->service->pcr_packet_period) {
00616 ts_st->service->pcr_packet_count = 0;
00617 write_pcr = 1;
00618 }
00619 }
00620
00621 if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
00622 (dts - (int64_t)ts->cur_pcr) > delay) {
00623
00624 if (write_pcr)
00625 mpegts_insert_pcr_only(s, st);
00626 else
00627 mpegts_insert_null_packet(s);
00628 continue;
00629 }
00630
00631
00632 q = buf;
00633 *q++ = 0x47;
00634 val = (ts_st->pid >> 8);
00635 if (is_start)
00636 val |= 0x40;
00637 *q++ = val;
00638 *q++ = ts_st->pid;
00639 ts_st->cc = (ts_st->cc + 1) & 0xf;
00640 *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
00641 if (write_pcr) {
00642
00643 if (ts->mux_rate > 1)
00644 pcr = ts->cur_pcr + (4+7)*8*90000LL / ts->mux_rate;
00645 else
00646 pcr = dts - delay;
00647 if (dts != AV_NOPTS_VALUE && dts < pcr)
00648 av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
00649 *q++ = 7;
00650 *q++ = 0x10;
00651 *q++ = pcr >> 25;
00652 *q++ = pcr >> 17;
00653 *q++ = pcr >> 9;
00654 *q++ = pcr >> 1;
00655 *q++ = (pcr & 1) << 7;
00656 *q++ = 0;
00657 }
00658 if (is_start) {
00659 int pes_extension = 0;
00660
00661 *q++ = 0x00;
00662 *q++ = 0x00;
00663 *q++ = 0x01;
00664 private_code = 0;
00665 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00666 if (st->codec->codec_id == CODEC_ID_DIRAC) {
00667 *q++ = 0xfd;
00668 } else
00669 *q++ = 0xe0;
00670 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
00671 (st->codec->codec_id == CODEC_ID_MP2 ||
00672 st->codec->codec_id == CODEC_ID_MP3)) {
00673 *q++ = 0xc0;
00674 } else {
00675 *q++ = 0xbd;
00676 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
00677 private_code = 0x20;
00678 }
00679 }
00680 header_len = 0;
00681 flags = 0;
00682 if (pts != AV_NOPTS_VALUE) {
00683 header_len += 5;
00684 flags |= 0x80;
00685 }
00686 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
00687 header_len += 5;
00688 flags |= 0x40;
00689 }
00690 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
00691 st->codec->codec_id == CODEC_ID_DIRAC) {
00692
00693 pes_extension = 1;
00694 flags |= 0x01;
00695
00696
00697
00698
00699
00700
00701 header_len += 3;
00702 }
00703 len = payload_size + header_len + 3;
00704 if (private_code != 0)
00705 len++;
00706 if (len > 0xffff)
00707 len = 0;
00708 *q++ = len >> 8;
00709 *q++ = len;
00710 val = 0x80;
00711
00712 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
00713 val |= 0x04;
00714 *q++ = val;
00715 *q++ = flags;
00716 *q++ = header_len;
00717 if (pts != AV_NOPTS_VALUE) {
00718 write_pts(q, flags >> 6, pts);
00719 q += 5;
00720 }
00721 if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
00722 write_pts(q, 1, dts);
00723 q += 5;
00724 }
00725 if (pes_extension && st->codec->codec_id == CODEC_ID_DIRAC) {
00726 flags = 0x01;
00727 *q++ = flags;
00728 *q++ = 0x80 | 0x01;
00729
00730
00731
00732
00733 *q++ = 0x00 | 0x60;
00734 }
00735 if (private_code != 0)
00736 *q++ = private_code;
00737 is_start = 0;
00738 }
00739
00740 header_len = q - buf;
00741
00742 len = TS_PACKET_SIZE - header_len;
00743 if (len > payload_size)
00744 len = payload_size;
00745 stuffing_len = TS_PACKET_SIZE - header_len - len;
00746 if (stuffing_len > 0) {
00747
00748 if (buf[3] & 0x20) {
00749
00750 afc_len = buf[4] + 1;
00751 memmove(buf + 4 + afc_len + stuffing_len,
00752 buf + 4 + afc_len,
00753 header_len - (4 + afc_len));
00754 buf[4] += stuffing_len;
00755 memset(buf + 4 + afc_len, 0xff, stuffing_len);
00756 } else {
00757
00758 memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
00759 buf[3] |= 0x20;
00760 buf[4] = stuffing_len - 1;
00761 if (stuffing_len >= 2) {
00762 buf[5] = 0x00;
00763 memset(buf + 6, 0xff, stuffing_len - 2);
00764 }
00765 }
00766 }
00767 memcpy(buf + TS_PACKET_SIZE - len, payload, len);
00768 payload += len;
00769 payload_size -= len;
00770 put_buffer(s->pb, buf, TS_PACKET_SIZE);
00771 ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
00772 }
00773 put_flush_packet(s->pb);
00774 }
00775
00776 static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
00777 {
00778 AVStream *st = s->streams[pkt->stream_index];
00779 int size = pkt->size;
00780 uint8_t *buf= pkt->data;
00781 uint8_t *data= NULL;
00782 MpegTSWriteStream *ts_st = st->priv_data;
00783 const uint64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE)*2;
00784 int64_t dts = AV_NOPTS_VALUE, pts = AV_NOPTS_VALUE;
00785
00786 if (pkt->pts != AV_NOPTS_VALUE)
00787 pts = pkt->pts + delay;
00788 if (pkt->dts != AV_NOPTS_VALUE)
00789 dts = pkt->dts + delay;
00790
00791 if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
00792 av_log(s, AV_LOG_ERROR, "first pts value must set\n");
00793 return -1;
00794 }
00795 ts_st->first_pts_check = 0;
00796
00797 if (st->codec->codec_id == CODEC_ID_H264) {
00798 const uint8_t *p = buf, *buf_end = p+size;
00799 uint32_t state = -1;
00800
00801 if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
00802 av_log(s, AV_LOG_ERROR, "h264 bitstream malformated, "
00803 "no startcode found, use -vbsf h264_mp4toannexb\n");
00804 return -1;
00805 }
00806
00807 do {
00808 p = ff_find_start_code(p, buf_end, &state);
00809
00810 } while (p < buf_end && (state & 0x1f) != 9 &&
00811 (state & 0x1f) != 5 && (state & 0x1f) != 1);
00812
00813 if ((state & 0x1f) != 9) {
00814 data = av_malloc(pkt->size+6);
00815 if (!data)
00816 return -1;
00817 memcpy(data+6, pkt->data, pkt->size);
00818 AV_WB32(data, 0x00000001);
00819 data[4] = 0x09;
00820 data[5] = 0xe0;
00821 buf = data;
00822 size = pkt->size+6;
00823 }
00824 } else if (st->codec->codec_id == CODEC_ID_AAC) {
00825 if (pkt->size < 2)
00826 return -1;
00827 if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
00828 ADTSContext *adts = ts_st->adts;
00829 int new_size;
00830 if (!adts) {
00831 av_log(s, AV_LOG_ERROR, "aac bitstream not in adts format "
00832 "and extradata missing\n");
00833 return -1;
00834 }
00835 new_size = ADTS_HEADER_SIZE+adts->pce_size+pkt->size;
00836 if ((unsigned)new_size >= INT_MAX)
00837 return -1;
00838 data = av_malloc(new_size);
00839 if (!data)
00840 return AVERROR(ENOMEM);
00841 ff_adts_write_frame_header(adts, data, pkt->size, adts->pce_size);
00842 if (adts->pce_size) {
00843 memcpy(data+ADTS_HEADER_SIZE, adts->pce_data, adts->pce_size);
00844 adts->pce_size = 0;
00845 }
00846 memcpy(data+ADTS_HEADER_SIZE+adts->pce_size, pkt->data, pkt->size);
00847 buf = data;
00848 size = new_size;
00849 }
00850 }
00851
00852 if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
00853
00854 mpegts_write_pes(s, st, buf, size, pts, dts);
00855 av_free(data);
00856 return 0;
00857 }
00858
00859 if (ts_st->payload_index + size > DEFAULT_PES_PAYLOAD_SIZE) {
00860 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
00861 ts_st->payload_pts, ts_st->payload_dts);
00862 ts_st->payload_index = 0;
00863 }
00864
00865 if (!ts_st->payload_index) {
00866 ts_st->payload_pts = pts;
00867 ts_st->payload_dts = dts;
00868 }
00869
00870 memcpy(ts_st->payload + ts_st->payload_index, buf, size);
00871 ts_st->payload_index += size;
00872
00873 av_free(data);
00874
00875 return 0;
00876 }
00877
00878 static int mpegts_write_end(AVFormatContext *s)
00879 {
00880 MpegTSWrite *ts = s->priv_data;
00881 MpegTSWriteStream *ts_st;
00882 MpegTSService *service;
00883 AVStream *st;
00884 int i;
00885
00886
00887 for(i = 0; i < s->nb_streams; i++) {
00888 st = s->streams[i];
00889 ts_st = st->priv_data;
00890 if (ts_st->payload_index > 0) {
00891 mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
00892 ts_st->payload_pts, ts_st->payload_dts);
00893 }
00894 av_freep(&ts_st->adts);
00895 }
00896 put_flush_packet(s->pb);
00897
00898 for(i = 0; i < ts->nb_services; i++) {
00899 service = ts->services[i];
00900 av_freep(&service->provider_name);
00901 av_freep(&service->name);
00902 av_free(service);
00903 }
00904 av_free(ts->services);
00905
00906 return 0;
00907 }
00908
00909 AVOutputFormat mpegts_muxer = {
00910 "mpegts",
00911 NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
00912 "video/x-mpegts",
00913 "ts,m2t",
00914 sizeof(MpegTSWrite),
00915 CODEC_ID_MP2,
00916 CODEC_ID_MPEG2VIDEO,
00917 mpegts_write_header,
00918 mpegts_write_packet,
00919 mpegts_write_end,
00920 };