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