00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avformat.h"
00022 #include "crc.h"
00023 #include <pthread.h>
00024 #include "libmyth/compat.h"
00025 #include "mpegts.h"
00026
00027
00028
00029
00030
00031 #define MAX_SCAN_PACKETS 32000
00032
00033
00034
00035 #define MAX_RESYNC_SIZE 4096
00036
00037 #define PMT_NOT_YET_FOUND 0
00038 #define PMT_NOT_IN_PAT 1
00039 #define PMT_FOUND 2
00040
00041 typedef struct PESContext PESContext;
00042 typedef struct SectionContext SectionContext;
00043
00044 static PESContext* add_pes_stream(MpegTSContext *ts, int pid, int stream_type);
00045 static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code);
00046 static AVStream *new_section_av_stream(SectionContext *sect, uint32_t code);
00047 static SectionContext *add_section_stream(MpegTSContext *ts, int pid, int stream_type);
00048 static void mpegts_cleanup_streams(MpegTSContext *ts);
00049 static int is_desired_stream(int type);
00050 static int find_in_list(const int *pids, int pid);
00051
00052 extern pthread_mutex_t avcodeclock;
00053
00054 enum MpegTSFilterType {
00055 MPEGTS_PES,
00056 MPEGTS_SECTION,
00057 };
00058
00059 typedef struct
00060 {
00061 char language[4];
00062 int comp_page;
00063 int anc_page;
00064 int sub_id;
00065 int txt_type;
00066
00067 int data_id;
00068 int carousel_id;
00069 int component_tag;
00070 } dvb_caption_info_t;
00071
00072 static int mpegts_parse_desc(dvb_caption_info_t *dvbci,
00073 uint8_t **p, uint8_t *p_end, int *stream_type);
00074
00075 typedef struct
00076 {
00077 int pid;
00078 int type;
00079 dvb_caption_info_t dvbci;
00080 } pmt_entry_t;
00081
00082 static int is_pat_same(MpegTSContext *mpegts_ctx,
00083 int *pmt_pnums, int *pmts_pids, uint pmt_count);
00084
00085 static void mpegts_add_stream(MpegTSContext *ts, pmt_entry_t* item);
00086 static int is_pmt_same(MpegTSContext *mpegts_ctx, pmt_entry_t* items,
00087 int item_cnt);
00088
00089 typedef void PESCallback(void *opaque, const uint8_t *buf, int len,
00090 int is_start, int64_t startpos);
00091
00092 typedef struct MpegTSPESFilter {
00093 PESCallback *pes_cb;
00094 void *opaque;
00095 } MpegTSPESFilter;
00096
00097 typedef void SectionCallback(void *opaque, const uint8_t *buf, int len);
00098
00099 typedef void SetServiceCallback(void *opaque, int ret);
00100
00101 typedef struct MpegTSSectionFilter {
00102 int section_index;
00103 int section_h_size;
00104 uint8_t *section_buf;
00105 int check_crc:1;
00106 int end_of_section_reached:1;
00107 SectionCallback *section_cb;
00108 void *opaque;
00109 } MpegTSSectionFilter;
00110
00111 typedef struct MpegTSFilter {
00112 int pid;
00113 int last_cc;
00114 enum MpegTSFilterType type;
00115 union {
00116 MpegTSPESFilter pes_filter;
00117 MpegTSSectionFilter section_filter;
00118 } u;
00119 } MpegTSFilter;
00120
00121 typedef struct MpegTSService {
00122 int running:1;
00123 int sid;
00124 int pid;
00125 char *provider_name;
00126 char *name;
00127 } MpegTSService;
00128
00130 #define PAT_MAX_PMT 128
00131
00133 #define PMT_PIDS_MAX 256
00134
00135 struct MpegTSContext {
00136
00137 AVFormatContext *stream;
00139 int raw_packet_size;
00141 int auto_guess;
00142 #if 0
00143 int set_service_ret;
00144 #endif
00145
00147 int mpeg2ts_raw;
00149 int mpeg2ts_compute_pcr;
00150
00151 int64_t cur_pcr;
00152 int pcr_incr;
00153 int pcr_pid;
00155
00156
00158 int scanning;
00160 int stop_parse;
00165 int pmt_scan_state;
00166
00168 AVPacket *pkt;
00169
00170
00171
00172
00173 #if 0
00174
00175 MpegTSFilter *sdt_filter;
00176 #endif
00177
00179 int nb_services;
00181 MpegTSService **services;
00182
00183 #if 0
00184
00185 SetServiceCallback *set_service_cb;
00186 void *set_service_opaque;
00187 #endif
00188
00190 MpegTSFilter *pat_filter;
00192 MpegTSFilter *pmt_filter;
00194 int req_sid;
00195
00197 MpegTSFilter *pids[NB_PID_MAX];
00198
00200 int pid_cnt;
00202 int pmt_pids[PMT_PIDS_MAX];
00203 };
00204
00205
00206
00207 enum MpegTSState {
00208 MPEGTS_HEADER = 0,
00209 MPEGTS_PESHEADER_FILL,
00210 MPEGTS_PAYLOAD,
00211 MPEGTS_SKIP,
00212 };
00213
00214
00215 #define PES_START_SIZE 9
00216 #define MAX_PES_HEADER_SIZE (9 + 255)
00217
00218 struct PESContext {
00219 int pid;
00220 int stream_type;
00221 MpegTSContext *ts;
00222 AVFormatContext *stream;
00223 AVStream *st;
00224 enum MpegTSState state;
00225
00226 int data_index;
00227 int total_size;
00228 int pes_header_size;
00229 int64_t pts, dts;
00230 uint8_t header[MAX_PES_HEADER_SIZE];
00231 int64_t startpos;
00232 };
00233
00234 extern AVInputFormat mpegts_demuxer;
00235
00236
00237 struct SectionContext {
00238 int pid;
00239 int stream_type;
00240 MpegTSContext *ts;
00241 AVFormatContext *stream;
00242 AVStream *st;
00243 };
00244
00245 static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter);
00246
00253 static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
00254 const uint8_t *buf, int buf_size, int is_start)
00255 {
00256 MpegTSSectionFilter *tss = &tss1->u.section_filter;
00257 int len;
00258
00259 if (is_start) {
00260 memcpy(tss->section_buf, buf, buf_size);
00261 tss->section_index = buf_size;
00262 tss->section_h_size = -1;
00263 tss->end_of_section_reached = 0;
00264 } else {
00265 if (tss->end_of_section_reached)
00266 return;
00267 len = 4096 - tss->section_index;
00268 if (buf_size < len)
00269 len = buf_size;
00270 memcpy(tss->section_buf + tss->section_index, buf, len);
00271 tss->section_index += len;
00272 }
00273
00274 while (1) {
00275
00276 if (tss->section_h_size == -1 && tss->section_index >= 3) {
00277 len = (((tss->section_buf[1] & 0xf) << 8) | tss->section_buf[2]) + 3;
00278 if (len > 4096)
00279 break;
00280 tss->section_h_size = len;
00281 }
00282
00283 if (tss->section_h_size == -1 || tss->section_index < tss->section_h_size)
00284 break;
00285
00286 if (!tss->check_crc || av_crc(av_crc04C11DB7, -1, tss->section_buf, tss->section_h_size) == 0)
00287 tss->section_cb(tss->opaque, tss->section_buf, tss->section_h_size);
00288
00289 if (tss->section_index > tss->section_h_size) {
00290 int left = tss->section_index - tss->section_h_size;
00291 memmove(tss->section_buf, tss->section_buf+tss->section_h_size, left);
00292 tss->section_index = left;
00293 tss->section_h_size = -1;
00294 } else {
00295 tss->end_of_section_reached = 1;
00296 break;
00297 }
00298 }
00299 }
00300
00301 MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
00302 SectionCallback *section_cb, void *opaque,
00303 int check_crc)
00304
00305 {
00306 MpegTSFilter *filter = ts->pids[pid];
00307 MpegTSSectionFilter *sec;
00308
00309 #ifdef DEBUG_SI
00310 av_log(ts->stream, AV_LOG_DEBUG, "Filter: pid=0x%x\n", pid);
00311 #endif
00312
00313 if (NULL!=filter) {
00314 #ifdef DEBUG_SI
00315 av_log(ts->stream, AV_LOG_DEBUG, "Filter Already Exists\n");
00316 #endif
00317 mpegts_close_filter(ts, filter);
00318 }
00319
00320 if (pid >= NB_PID_MAX || ts->pids[pid])
00321 return NULL;
00322 filter = av_mallocz(sizeof(MpegTSFilter));
00323 if (!filter)
00324 return NULL;
00325 ts->pids[pid] = filter;
00326 filter->type = MPEGTS_SECTION;
00327 filter->pid = pid;
00328 filter->last_cc = -1;
00329 sec = &filter->u.section_filter;
00330 sec->section_cb = section_cb;
00331 sec->opaque = opaque;
00332 sec->section_buf = av_malloc(MAX_SECTION_SIZE);
00333 sec->check_crc = check_crc;
00334 if (!sec->section_buf) {
00335 av_free(filter);
00336 return NULL;
00337 }
00338 return filter;
00339 }
00340
00341 MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
00342 PESCallback *pes_cb,
00343 void *opaque)
00344 {
00345 MpegTSFilter *filter;
00346 MpegTSPESFilter *pes;
00347
00348 if (pid >= NB_PID_MAX || ts->pids[pid])
00349 return NULL;
00350 filter = av_mallocz(sizeof(MpegTSFilter));
00351 if (!filter)
00352 return NULL;
00353 ts->pids[pid] = filter;
00354 filter->type = MPEGTS_PES;
00355 filter->pid = pid;
00356 filter->last_cc = -1;
00357 pes = &filter->u.pes_filter;
00358 pes->pes_cb = pes_cb;
00359 pes->opaque = opaque;
00360 return filter;
00361 }
00362
00363 void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
00364 {
00365 int pid;
00366
00367 if (!ts || !filter)
00368 return;
00369
00370 pid = filter->pid;
00371
00372 #ifdef DEBUG_SI
00373 av_log(NULL, AV_LOG_DEBUG, "Closing Filter: pid=0x%x\n", pid);
00374 #endif
00375 if (filter == ts->pmt_filter)
00376 {
00377 av_log(NULL, AV_LOG_DEBUG, "Closing PMT Filter: pid=0x%x\n", pid);
00378 ts->pmt_filter = NULL;
00379 }
00380 if (filter == ts->pat_filter)
00381 {
00382 av_log(NULL, AV_LOG_DEBUG, "Closing PAT Filter: pid=0x%x\n", pid);
00383 ts->pat_filter = NULL;
00384 }
00385
00386 if (filter->type == MPEGTS_SECTION)
00387 av_freep(&filter->u.section_filter.section_buf);
00388 else if (filter->type == MPEGTS_PES)
00389 av_freep(&filter->u.pes_filter.opaque);
00390
00391 av_free(filter);
00392 ts->pids[pid] = NULL;
00393 }
00394
00395 static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
00396 int stat[packet_size];
00397 int i;
00398 int x=0;
00399 int best_score=0;
00400
00401 memset(stat, 0, packet_size*sizeof(int));
00402
00403 for(x=i=0; i<size; i++){
00404 if(buf[i] == 0x47){
00405 stat[x]++;
00406 if(stat[x] > best_score){
00407 best_score= stat[x];
00408 if(index) *index= x;
00409 }
00410 }
00411
00412 x++;
00413 if(x == packet_size) x= 0;
00414 }
00415
00416 return best_score;
00417 }
00418
00419
00420 static int get_packet_size(const uint8_t *buf, int size)
00421 {
00422 int score, fec_score, dvhs_score;
00423
00424 if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
00425 return -1;
00426
00427 score = analyze(buf, size, TS_PACKET_SIZE, NULL);
00428 dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
00429 fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
00430
00431
00432
00433 if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
00434 else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
00435 else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
00436 else return -1;
00437 }
00438
00439 typedef struct SectionHeader {
00440 uint8_t tid;
00441 uint16_t id;
00442 uint8_t version;
00443 uint8_t sec_num;
00444 uint8_t last_sec_num;
00445 } SectionHeader;
00446
00447 static inline int get8(const uint8_t **pp, const uint8_t *p_end)
00448 {
00449 const uint8_t *p;
00450 int c;
00451
00452 p = *pp;
00453 if (p >= p_end)
00454 return -1;
00455 c = *p++;
00456 *pp = p;
00457 return c;
00458 }
00459
00460 static inline int get16(const uint8_t **pp, const uint8_t *p_end)
00461 {
00462 const uint8_t *p;
00463 int c;
00464
00465 p = *pp;
00466 if ((p + 1) >= p_end)
00467 return -1;
00468 c = (p[0] << 8) | p[1];
00469 p += 2;
00470 *pp = p;
00471 return c;
00472 }
00473
00474
00475 static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
00476 {
00477 int len;
00478 const uint8_t *p;
00479 char *str;
00480
00481 p = *pp;
00482 len = get8(&p, p_end);
00483 if (len < 0)
00484 return NULL;
00485 if ((p + len) > p_end)
00486 return NULL;
00487 str = av_malloc(len + 1);
00488 if (!str)
00489 return NULL;
00490 memcpy(str, p, len);
00491 str[len] = '\0';
00492 p += len;
00493 *pp = p;
00494 return str;
00495 }
00496
00497 static int parse_section_header(SectionHeader *h,
00498 const uint8_t **pp, const uint8_t *p_end)
00499 {
00500 int val;
00501
00502 val = get8(pp, p_end);
00503 if (val < 0)
00504 return -1;
00505 h->tid = val;
00506 *pp += 2;
00507 val = get16(pp, p_end);
00508 if (val < 0)
00509 return -1;
00510 h->id = val;
00511 val = get8(pp, p_end);
00512 if (val < 0)
00513 return -1;
00514 h->version = (val >> 1) & 0x1f;
00515 val = get8(pp, p_end);
00516 if (val < 0)
00517 return -1;
00518 h->sec_num = val;
00519 val = get8(pp, p_end);
00520 if (val < 0)
00521 return -1;
00522 h->last_sec_num = val;
00523
00524 #ifdef DEBUG_SI
00525 av_log(NULL, AV_LOG_DEBUG, "sid=0x%x sec_num=%d/%d\n",
00526 h->id, h->sec_num, h->last_sec_num);
00527 #endif
00528 return 0;
00529 }
00530
00531 static MpegTSService *new_service(MpegTSContext *ts, int sid, int pid,
00532 char *provider_name, char *name)
00533 {
00534 MpegTSService *service;
00535
00536 #ifdef DEBUG_SI
00537 av_log(ts->stream, AV_LOG_DEBUG, "new_service: "
00538 "sid=0x%04x provider='%s' name='%s'\n",
00539 sid, provider_name, name);
00540 #endif
00541
00542 service = av_mallocz(sizeof(MpegTSService));
00543 if (!service)
00544 return NULL;
00545 service->sid = sid;
00546 service->pid = pid;
00547 service->provider_name = provider_name;
00548 service->name = name;
00549 dynarray_add(&ts->services, &ts->nb_services, service);
00550 return service;
00551 }
00552
00553 static int mpegts_parse_program_info_length(uint8_t **p, uint8_t *p_end)
00554 {
00555 int program_info_length = get16(p, p_end);
00556 if (program_info_length < 0)
00557 return -1;
00558 program_info_length &= 0xfff;
00559 *p += program_info_length;
00560 if (*p >= p_end)
00561 return -1;
00562 return program_info_length;
00563 }
00564
00565 static int find_in_list(const int *pids, int pid) {
00566 int i;
00567 for (i=0; i<PMT_PIDS_MAX; i++)
00568 if (pids[i]==pid)
00569 return i;
00570 return -1;
00571 }
00572
00573 static int mpegts_parse_pcrpid(MpegTSContext *mpegts_ctx,
00574 uint8_t **p, uint8_t *p_end)
00575 {
00576 int pcr_pid = get16(p, p_end);
00577 if (pcr_pid < 0)
00578 return -1;
00579 pcr_pid &= 0x1fff;
00580 mpegts_ctx->pcr_pid = pcr_pid;
00581 #ifdef DEBUG_SI
00582 av_log(NULL, AV_LOG_DEBUG, "pcr_pid=0x%x\n", pcr_pid);
00583 #endif
00584 return pcr_pid;
00585 }
00586
00587 #define HANDLE_PMT_ERROR(MSG) \
00588 do { av_log(NULL, AV_LOG_ERROR, MSG); return; } while (0)
00589
00590 #define HANDLE_PMT_PARSE_ERROR(PMSG) \
00591 HANDLE_PMT_ERROR("Something went terribly wrong in PMT parsing" \
00592 " when looking at " PMSG "\n")
00593
00594 static void pmt_cb(void *opaque, const uint8_t *section, int section_len)
00595 {
00596 MpegTSContext *mpegts_ctx = opaque;
00597 const uint8_t *p = section, *p_end = section + section_len - 4;
00598 SectionHeader header;
00599
00600 int last_item = 0;
00601 int desc_count = 0;
00602 pmt_entry_t items[PMT_PIDS_MAX];
00603 bzero(&items, sizeof(pmt_entry_t) * PMT_PIDS_MAX);
00604
00605 mpegts_cleanup_streams(mpegts_ctx);
00606
00607 #ifdef DEBUG_SI
00608 av_log(ts->stream, AV_LOG_DEBUG, "PMT: len %i\n", section_len);
00609 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
00610 #endif
00611
00612 if (parse_section_header(&header, &p, p_end) < 0)
00613 HANDLE_PMT_PARSE_ERROR("section header");
00614
00615
00616 if (mpegts_ctx->req_sid >= 0 && header.id != mpegts_ctx->req_sid)
00617 {
00618 #ifdef DEBUG_SI
00619 av_log(NULL, AV_LOG_DEBUG, "We are looking for program 0x%x, not 0x%x",
00620 mpegts_ctx->req_sid, header.id);
00621 #endif
00622 return;
00623 }
00624
00625
00626 if (header.tid != PMT_TID)
00627 HANDLE_PMT_ERROR("pmt_cb() got a TS packet that doesn't have PMT TID!");
00628
00629
00630 if (mpegts_parse_pcrpid(mpegts_ctx, &p, p_end) < 0)
00631 HANDLE_PMT_PARSE_ERROR("PCR PID");
00632
00633
00634 if (mpegts_parse_program_info_length(&p, p_end) < 0)
00635 HANDLE_PMT_PARSE_ERROR("program info");
00636
00637
00638 while (p < p_end)
00639 {
00640 dvb_caption_info_t dvbci;
00641 int stream_type = get8(&p, p_end);
00642 int pid = get16(&p, p_end);
00643 int desc_ok = mpegts_parse_desc(&dvbci, &p, p_end, &stream_type);
00644 if ((stream_type < 0) || (pid < 0) || (desc_ok < 0))
00645 {
00646 av_log(NULL, AV_LOG_ERROR,
00647 "Something went terribly wrong in PMT parsing\n"
00648 " when looking at descriptors (0x%x 0x%x)\n",
00649 stream_type, pid & 0x1fff);
00650 break;
00651 }
00652 pid &= 0x1fff;
00653 desc_count++;
00654
00655 if (dvbci.sub_id && (stream_type == STREAM_TYPE_PRIVATE_DATA))
00656 stream_type = STREAM_TYPE_SUBTITLE_DVB;
00657
00658 if (dvbci.txt_type && (stream_type == STREAM_TYPE_PRIVATE_DATA))
00659 stream_type = STREAM_TYPE_VBI_DVB;
00660
00661 if ((dvbci.component_tag >= 0) && (stream_type == STREAM_TYPE_PRIVATE_DATA))
00662 {
00663
00664 if (dvbci.component_tag == 0x0a)
00665 stream_type = STREAM_TYPE_AUDIO_MPEG2;
00666 else if (dvbci.component_tag == 0x52 && desc_count == 1)
00667 stream_type = STREAM_TYPE_VIDEO_MPEG2;
00668 }
00669
00670 #ifdef DEBUG_SI
00671 av_log(ts->stream, AV_LOG_DEBUG, "pcr_pid=0x%x\n", pcr_pid);
00672 #endif
00673
00674 if (is_desired_stream(stream_type))
00675 {
00676
00677 if (last_item < PMT_PIDS_MAX)
00678 {
00679 items[last_item].pid = pid;
00680 items[last_item].type = stream_type;
00681 memcpy(&items[last_item].dvbci, &dvbci,
00682 sizeof(dvb_caption_info_t));
00683 last_item++;
00684 }
00685 else
00686 {
00687 av_log(NULL, AV_LOG_DEBUG,
00688 "Could not add new pid 0x%x, i = %i, "
00689 "would cause overrun\n", pid, last_item);
00690 assert(0);
00691 }
00692 }
00693 }
00694
00695
00696
00697
00698 if (!is_pmt_same(mpegts_ctx, items, last_item))
00699 {
00700 AVFormatContext *avctx = mpegts_ctx->stream;
00701 int idx;
00702
00703 av_read_frame_flush(avctx);
00704
00705 for (idx = mpegts_ctx->pid_cnt-1; idx>=0; idx--)
00706 av_remove_stream(mpegts_ctx->stream, mpegts_ctx->pmt_pids[idx], 1);
00707
00708
00709 for (idx = 0; idx < last_item; idx++)
00710 mpegts_add_stream(mpegts_ctx, &items[idx]);
00711
00712
00713 void *tmp0 = avctx->cur_pmt_sect;
00714 void *tmp1 = av_malloc(section_len);
00715 memcpy(tmp1, section, section_len);
00716 avctx->cur_pmt_sect = (uint8_t*) tmp1;
00717 avctx->cur_pmt_sect_len = section_len;
00718 if (tmp0)
00719 av_free(tmp0);
00720
00721
00722 if (avctx->streams_changed)
00723 {
00724 av_log(NULL, AV_LOG_DEBUG, "streams_changed()\n");
00725 avctx->streams_changed(avctx->stream_change_data);
00726 }
00727 }
00728
00729
00730 if (mpegts_ctx->scanning)
00731 {
00732 #ifdef DEBUG_SI
00733 av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
00734 desc_tag, desc_len);
00735 #endif
00736 mpegts_ctx->pmt_scan_state = PMT_FOUND;
00737 mpegts_ctx->stop_parse = 1;
00738 }
00739 }
00740
00741 static int is_pat_same(MpegTSContext *mpegts_ctx,
00742 int *pmt_pnums, int *pmt_pids, uint pmt_count)
00743 {
00744 int idx;
00745 if (mpegts_ctx->nb_services != pmt_count)
00746 return 0;
00747
00748 for (idx = 0; idx < pmt_count; idx++)
00749 {
00750 if ((mpegts_ctx->services[idx]->sid != pmt_pnums[idx]) ||
00751 (mpegts_ctx->services[idx]->pid != pmt_pids[idx]))
00752 return 0;
00753 }
00754 return 1;
00755 }
00756
00757 static int is_pmt_same(MpegTSContext *mpegts_ctx,
00758 pmt_entry_t* items, int item_cnt)
00759 {
00760 int idx;
00761 if (mpegts_ctx->pid_cnt != item_cnt)
00762 {
00763 #ifdef DEBUG_SI
00764 av_log(NULL, AV_LOG_DEBUG, "mpegts_ctx->pid_cnt=%d != item_cnt=%d\n",
00765 mpegts_ctx->pid_cnt, item_cnt);
00766 #endif
00767 return 0;
00768 }
00769 for (idx = 0; idx < item_cnt; idx++)
00770 {
00771
00772 int loc = find_in_list(mpegts_ctx->pmt_pids, items[idx].pid);
00773 if (loc < 0)
00774 {
00775 #ifdef DEBUG_SI
00776 av_log(NULL, AV_LOG_DEBUG,
00777 "find_in_list(..,[%d].pid=%d) => -1\n"
00778 "is_pmt_same() => false\n",
00779 idx, items[idx].pid);
00780 #endif
00781 return 0;
00782 }
00783
00784
00785 MpegTSFilter *tss = mpegts_ctx->pids[items[idx].pid];
00786 if (!tss)
00787 {
00788 #ifdef DEBUG_SI
00789 av_log(NULL, AV_LOG_DEBUG,
00790 "mpegts_ctx->pids[items[%d].pid=%d] => null\n"
00791 "is_pmt_same() => false\n",
00792 idx, items[idx].pid);
00793 #endif
00794 return 0;
00795 }
00796 if (tss->type == MPEGTS_PES)
00797 {
00798 PESContext *pes = (PESContext*) tss->u.pes_filter.opaque;
00799 if (!pes)
00800 {
00801 #ifdef DEBUG_SI
00802 av_log(NULL, AV_LOG_DEBUG, "pes == null, where idx %d\n"
00803 "is_pmt_same() => false\n", idx);
00804 #endif
00805 return 0;
00806 }
00807 if (pes->stream_type != items[idx].type)
00808 {
00809 #ifdef DEBUG_SI
00810 av_log(NULL, AV_LOG_DEBUG,
00811 "pes->stream_type != items[%d].type\n"
00812 "is_pmt_same() => false\n", idx);
00813 #endif
00814 return 0;
00815 }
00816 }
00817 else if (tss->type == MPEGTS_SECTION)
00818 {
00819 SectionContext *sect = (SectionContext*) tss->u.section_filter.opaque;
00820 if (!sect)
00821 {
00822 #ifdef DEBUG_SI
00823 av_log(NULL, AV_LOG_DEBUG, "sect == null, where idx %d\n"
00824 "is_pmt_same() => false\n", idx);
00825 #endif
00826 return 0;
00827 }
00828 if (sect->stream_type != items[idx].type)
00829 {
00830 #ifdef DEBUG_SI
00831 av_log(NULL, AV_LOG_DEBUG,
00832 "sect->stream_type != items[%d].type\n"
00833 "is_pmt_same() => false\n", idx);
00834 #endif
00835 return 0;
00836 }
00837 }
00838 else
00839 {
00840 #ifdef DEBUG_SI
00841 av_log(NULL, AV_LOG_DEBUG,
00842 "tss->type != MPEGTS_PES, where idx %d\n"
00843 "is_pmt_same() => false\n", idx);
00844 #endif
00845 return 0;
00846 }
00847 }
00848 #ifdef DEBUG_SI
00849 av_log(NULL, AV_LOG_DEBUG, "is_pmt_same() => true\n", idx);
00850 #endif
00851 return 1;
00852 }
00853
00854 static int is_desired_stream(int stream_type)
00855 {
00856 int val = 0;
00857 switch (stream_type)
00858 {
00859 case STREAM_TYPE_AUDIO_MPEG1:
00860 case STREAM_TYPE_AUDIO_MPEG2:
00861 case STREAM_TYPE_VIDEO_MPEG1:
00862 case STREAM_TYPE_VIDEO_MPEG2:
00863 case STREAM_TYPE_VIDEO_MPEG4:
00864 case STREAM_TYPE_VIDEO_H264:
00865 case STREAM_TYPE_VIDEO_VC1:
00866 case STREAM_TYPE_AUDIO_AAC:
00867 case STREAM_TYPE_AUDIO_AC3:
00868 case STREAM_TYPE_AUDIO_DTS:
00869 case STREAM_TYPE_PRIVATE_DATA:
00870 case STREAM_TYPE_VBI_DVB:
00871 case STREAM_TYPE_SUBTITLE_DVB:
00872 case STREAM_TYPE_DSMCC_B:
00873 val = 1;
00874 break;
00875 default:
00876
00877 break;
00878 }
00879 return val;
00880 }
00881
00882 static int mpegts_parse_desc(dvb_caption_info_t *dvbci,
00883 uint8_t **p, uint8_t *p_end, int *stream_type)
00884 {
00885 const uint8_t *desc_list_end, *desc_end;
00886 int desc_list_len, desc_len, desc_tag;
00887
00888 bzero(dvbci, sizeof(dvb_caption_info_t));
00889 dvbci->component_tag = -1;
00890
00891 desc_list_len = get16(p, p_end);
00892 if (desc_list_len < 0)
00893 return -1;
00894 desc_list_len &= 0xfff;
00895 desc_list_end = *p + desc_list_len;
00896 if (desc_list_end > p_end)
00897 return -1;
00898 while (*p < desc_list_end)
00899 {
00900 desc_tag = get8(p, desc_list_end);
00901 if (desc_tag < 0)
00902 break;
00903 if (*stream_type == STREAM_TYPE_PRIVATE_DATA) {
00904 if((desc_tag == 0x6A) || (desc_tag == 0x7A)) {
00905
00906 *stream_type = STREAM_TYPE_AUDIO_AC3;
00907 } else if(desc_tag == 0x7B) {
00908
00909 *stream_type = STREAM_TYPE_AUDIO_DTS;
00910 }
00911 }
00912 desc_len = get8(p, desc_list_end);
00913 desc_end = *p + desc_len;
00914 if (desc_end > desc_list_end)
00915 break;
00916 #ifdef DEBUG_SI
00917 av_log(NULL, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
00918 #endif
00919 switch (desc_tag)
00920 {
00921 case DVB_SUBT_DESCID:
00922 dvbci->language[0] = get8(p, desc_end);
00923 dvbci->language[1] = get8(p, desc_end);
00924 dvbci->language[2] = get8(p, desc_end);
00925 dvbci->language[3] = 0;
00926 get8(p, desc_end);
00927 dvbci->comp_page = get16(p, desc_end);
00928 dvbci->anc_page = get16(p, desc_end);
00929 dvbci->sub_id = (dvbci->anc_page << 16) | dvbci->comp_page;
00930 break;
00931 case 0x0a:
00932 dvbci->language[0] = get8(p, desc_end);
00933 dvbci->language[1] = get8(p, desc_end);
00934 dvbci->language[2] = get8(p, desc_end);
00935 dvbci->language[3] = 0;
00936 break;
00937 case DVB_BROADCAST_ID:
00938 dvbci->data_id = get16(p, desc_end);
00939 break;
00940 case DVB_CAROUSEL_ID:
00941 {
00942 int carId = 0;
00943 carId = get8(p, desc_end);
00944 carId = (carId << 8) | get8(p, desc_end);
00945 carId = (carId << 8) | get8(p, desc_end);
00946 carId = (carId << 8) | get8(p, desc_end);
00947 dvbci->carousel_id = carId;
00948 }
00949 break;
00950 case DVB_DATA_STREAM:
00951 dvbci->component_tag = get8(p, desc_end);
00952 break;
00953 case DVB_VBI_DESCID:
00954 dvbci->language[0] = get8(p, desc_end);
00955 dvbci->language[1] = get8(p, desc_end);
00956 dvbci->language[2] = get8(p, desc_end);
00957 dvbci->txt_type = (get8(p, desc_end)) >> 3;
00958 break;
00959 default:
00960 break;
00961 }
00962 *p = desc_end;
00963 }
00964 *p = desc_list_end;
00965 return 1;
00966 }
00967
00968 static void mpegts_cleanup_streams(MpegTSContext *ts)
00969 {
00970 int i;
00971 int orig_pid_cnt = ts->pid_cnt;
00972 for (i=0; i<ts->pid_cnt; i++)
00973 {
00974 if (!ts->pids[ts->pmt_pids[i]])
00975 {
00976 mpegts_remove_stream(ts, ts->pmt_pids[i]);
00977 i--;
00978 }
00979 }
00980 if (orig_pid_cnt != ts->pid_cnt)
00981 {
00982 av_log(NULL, AV_LOG_DEBUG,
00983 "mpegts_cleanup_streams: pid_cnt bfr %d aft %d\n",
00984 orig_pid_cnt, ts->pid_cnt);
00985 }
00986 }
00987
00988 static void mpegts_add_stream(MpegTSContext *ts, pmt_entry_t* item)
00989 {
00990
00991 av_log(NULL, AV_LOG_DEBUG,
00992 "mpegts_add_stream: at pid 0x%x with type %i\n", item->pid, item->type);
00993
00994 if (ts->pid_cnt < PMT_PIDS_MAX)
00995 {
00996 if (item->type == STREAM_TYPE_DSMCC_B)
00997 {
00998 SectionContext *sect = NULL;
00999 AVStream *st = NULL;
01000 sect = add_section_stream(ts, item->pid, item->type);
01001 if (!sect)
01002 {
01003 av_log(NULL, AV_LOG_ERROR, "mpegts_add_stream: "
01004 "error creating Section context for pid 0x%x with type %i\n",
01005 item->pid, item->type);
01006 return;
01007 }
01008
01009 st = new_section_av_stream(sect, 0);
01010 if (!st)
01011 {
01012 av_log(NULL, AV_LOG_ERROR, "mpegts_add_stream: "
01013 "error creating A/V stream for pid 0x%x with type %i\n",
01014 item->pid, item->type);
01015 return;
01016 }
01017
01018 st->component_tag = item->dvbci.component_tag;
01019 st->codec->flags = item->dvbci.data_id;
01020 st->codec->sub_id = item->dvbci.carousel_id;
01021
01022 ts->pmt_pids[ts->pid_cnt] = item->pid;
01023 ts->pid_cnt++;
01024
01025 av_log(NULL, AV_LOG_DEBUG, "mpegts_add_stream: "
01026 "stream #%d, has id 0x%x and codec %s, type %s at 0x%x\n",
01027 st->index, st->id, codec_id_string(st->codec->codec_id),
01028 codec_type_string(st->codec->codec_type), st);
01029 } else {
01030 PESContext *pes = NULL;
01031 AVStream *st = NULL;
01032 pes = add_pes_stream(ts, item->pid, item->type);
01033 if (!pes)
01034 {
01035 av_log(NULL, AV_LOG_ERROR, "mpegts_add_stream: "
01036 "error creating PES context for pid 0x%x with type %i\n",
01037 item->pid, item->type);
01038 return;
01039 }
01040
01041
01042 st = new_pes_av_stream(pes, item->dvbci.language[0] ? 0x1c0 : 0);
01043 if (!st)
01044 {
01045 av_log(NULL, AV_LOG_ERROR, "mpegts_add_stream: "
01046 "error creating A/V stream for pid 0x%x with type %i\n",
01047 item->pid, item->type);
01048 return;
01049 }
01050
01051 ts->pmt_pids[ts->pid_cnt] = item->pid;
01052 ts->pid_cnt++;
01053
01054 if (item->dvbci.language[0])
01055 memcpy(st->language, item->dvbci.language, sizeof(char) * 4);
01056
01057 if (item->dvbci.sub_id && (item->type == STREAM_TYPE_SUBTITLE_DVB))
01058 st->codec->sub_id = item->dvbci.sub_id;
01059
01060 st->component_tag = item->dvbci.component_tag;
01061
01062 av_log(NULL, AV_LOG_DEBUG, "mpegts_add_stream: "
01063 "stream #%d, has id 0x%x and codec %s, type %s at 0x%x\n",
01064 st->index, st->id, codec_id_string(st->codec->codec_id),
01065 codec_type_string(st->codec->codec_type), st);
01066 }
01067 }
01068 else
01069 {
01070 av_log(NULL, AV_LOG_ERROR,
01071 "ERROR: adding pes stream at pid 0x%x, pid_cnt = %i\n",
01072 item->pid, ts->pid_cnt);
01073 }
01074 }
01075
01076 void mpegts_remove_stream(MpegTSContext *ts, int pid)
01077 {
01078 av_log(NULL, AV_LOG_DEBUG, "mpegts_remove_stream 0x%x\n", pid);
01079 if (ts->pids[pid])
01080 {
01081 av_log(NULL, AV_LOG_DEBUG, "closing filter for pid 0x%x\n", pid);
01082 mpegts_close_filter(ts, ts->pids[pid]);
01083 }
01084 int indx = find_in_list(ts->pmt_pids, pid);
01085 if (indx >= 0)
01086 {
01087 memmove(ts->pmt_pids+indx, ts->pmt_pids+indx+1, PMT_PIDS_MAX-indx-1);
01088 ts->pmt_pids[PMT_PIDS_MAX-1] = 0;
01089 ts->pid_cnt--;
01090 }
01091 else
01092 {
01093 av_log(NULL, AV_LOG_DEBUG, "ERROR: closing filter for pid 0x%x, indx = %i\n", pid, indx);
01094 }
01095 }
01096
01097 static void pat_cb(void *opaque, const uint8_t *section, int section_len)
01098 {
01099 MpegTSContext *ts = opaque;
01100 SectionHeader h1, *h = &h1;
01101 const uint8_t *p, *p_end;
01102 char buf[256];
01103
01104 int pmt_pnums[PAT_MAX_PMT];
01105 int pmt_pids[PAT_MAX_PMT];
01106 uint pmt_count = 0;
01107 int i;
01108
01109 #ifdef DEBUG_SI
01110 av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n");
01111 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
01112 #endif
01113 p_end = section + section_len - 4;
01114 p = section;
01115 if (parse_section_header(h, &p, p_end) < 0)
01116 return;
01117 if (h->tid != PAT_TID)
01118 return;
01119
01120 for (i = 0; i < PAT_MAX_PMT; ++i)
01121 {
01122 pmt_pnums[i] = get16(&p, p_end);
01123 if (pmt_pnums[i] < 0)
01124 break;
01125
01126 pmt_pids[i] = get16(&p, p_end) & 0x1fff;
01127 if (pmt_pids[i] < 0)
01128 break;
01129
01130 if (pmt_pids[i] == 0x0)
01131 {
01132 av_log(NULL, AV_LOG_ERROR, "Invalid PAT ignored "
01133 "MPEG Program Number=0x%x pid=0x%x req_sid=0x%x\n",
01134 pmt_pnums[i], pmt_pids[i], ts->req_sid);
01135 return;
01136 }
01137
01138 pmt_count++;
01139
01140 #ifdef DEBUG_SI
01141 av_log(ts->stream, AV_LOG_DEBUG,
01142 "MPEG Program Number=0x%x pid=0x%x req_sid=0x%x\n",
01143 pmt_pnums[i], pmt_pids[i], ts->req_sid);
01144 #endif
01145 }
01146
01147 if (!is_pat_same(ts, pmt_pnums, pmt_pids, pmt_count))
01148 {
01149 #ifdef DEBUG_SI
01150 av_log(NULL, AV_LOG_DEBUG, "New PAT!\n");
01151 #endif
01152
01153 if (ts->nb_services)
01154 {
01155 for (i = ts->nb_services - 1; i >= 0; --i)
01156 {
01157 av_free(ts->services[i]->provider_name);
01158 av_free(ts->services[i]->name);
01159 av_free(ts->services[i]);
01160 ts->services[i] = NULL;
01161 }
01162 ts->nb_services = 0;
01163 ts->services = NULL;
01164 }
01165
01166
01167 for (i = 0; i < pmt_count; ++i)
01168 {
01169 snprintf(buf, sizeof(buf), "MPEG Program %x", pmt_pnums[i]);
01170 new_service(ts, pmt_pnums[i], pmt_pids[i],
01171 av_strdup(""), av_strdup(buf));
01172 }
01173 }
01174
01175 int found = 0;
01176 for (i = 0; i < pmt_count; ++i)
01177 {
01178
01179
01180 if (ts->req_sid == pmt_pnums[i])
01181 {
01182 #ifdef DEBUG_SI
01183 av_log(NULL, AV_LOG_DEBUG, "Found program number!\n");
01184 #endif
01185
01186 if (ts->pmt_filter)
01187 {
01188 MpegTSFilter *f = ts->pmt_filter;
01189 MpegTSSectionFilter *sec = &f->u.section_filter;
01190
01191 if ((f->pid != pmt_pids[i]) ||
01192 (f->type != MPEGTS_SECTION) ||
01193 (sec->section_cb != pmt_cb) ||
01194 (sec->opaque != ts))
01195 {
01196 mpegts_close_filter(ts, ts->pmt_filter);
01197 ts->pmt_filter = NULL;
01198 }
01199 }
01200
01201
01202 if (!ts->pmt_filter)
01203 {
01204 ts->pmt_filter = mpegts_open_section_filter(
01205 ts, pmt_pids[i], pmt_cb, ts, 1);
01206 }
01207
01208 found = 1;
01209 }
01210 }
01211
01212
01213
01214
01215 if (ts->req_sid < 0 && ts->scanning)
01216 {
01217 #ifdef DEBUG_SI
01218 av_log(NULL, AV_LOG_DEBUG, "Found PAT, ending scan\n");
01219 #endif
01220 ts->stop_parse = 1;
01221 }
01222
01223
01224
01225
01226 if (ts->req_sid >= 0 && !found)
01227 {
01228 #ifdef DEBUG_SI
01229 av_log(NULL, AV_LOG_DEBUG, "Program 0x%x is not in PAT, ending scan\n",
01230 ts->req_sid);
01231 #endif
01232 ts->pmt_scan_state = PMT_NOT_IN_PAT;
01233 ts->stop_parse = 1;
01234 }
01235 }
01236
01237 #if 0
01238
01239 static void pat_scan_cb(void *opaque, const uint8_t *section, int section_len)
01240 {
01241 MpegTSContext *ts = opaque;
01242 SectionHeader h1, *h = &h1;
01243 const uint8_t *p, *p_end;
01244 int sid, pmt_pid;
01245 char *provider_name, *name;
01246 char buf[256];
01247
01248 #ifdef DEBUG_SI
01249 av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n");
01250 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
01251 #endif
01252 p_end = section + section_len - 4;
01253 p = section;
01254 if (parse_section_header(h, &p, p_end) < 0)
01255 return;
01256 if (h->tid != PAT_TID)
01257 return;
01258
01259 for(;;) {
01260 sid = get16(&p, p_end);
01261 if (sid < 0)
01262 break;
01263 pmt_pid = get16(&p, p_end) & 0x1fff;
01264 if (pmt_pid < 0)
01265 break;
01266 #ifdef DEBUG_SI
01267 av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
01268 #endif
01269 if (sid == 0x0000) {
01270
01271 } else {
01272
01273 snprintf(buf, sizeof(buf), "Service %x\n", sid);
01274 name = av_strdup(buf);
01275 provider_name = av_strdup("");
01276 if (name && provider_name) {
01277 new_service(ts, sid, provider_name, name);
01278 } else {
01279 av_freep(&name);
01280 av_freep(&provider_name);
01281 }
01282 }
01283 }
01284 ts->stop_parse = 1;
01285
01286
01287 mpegts_close_filter(ts, ts->pat_filter);
01288 ts->pat_filter = NULL;
01289 #ifdef DEBUG_SI
01290 av_log(NULL, AV_LOG_DEBUG, "end of scan PAT\n");
01291 #endif
01292 }
01293 #endif
01294
01295 #if 0
01296 static void sdt_cb(void *opaque, const uint8_t *section, int section_len)
01297 {
01298 MpegTSContext *ts = opaque;
01299 SectionHeader h1, *h = &h1;
01300 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
01301 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
01302 char *name, *provider_name;
01303
01304 #ifdef DEBUG_SI
01305 av_log(ts->stream, AV_LOG_DEBUG, "SDT:\n");
01306 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
01307 #endif
01308
01309 p_end = section + section_len - 4;
01310 p = section;
01311 if (parse_section_header(h, &p, p_end) < 0)
01312 return;
01313 if (h->tid != SDT_TID)
01314 return;
01315 onid = get16(&p, p_end);
01316 if (onid < 0)
01317 return;
01318 val = get8(&p, p_end);
01319 if (val < 0)
01320 return;
01321 for(;;) {
01322 sid = get16(&p, p_end);
01323 if (sid < 0)
01324 break;
01325 val = get8(&p, p_end);
01326 if (val < 0)
01327 break;
01328 desc_list_len = get16(&p, p_end) & 0xfff;
01329 if (desc_list_len < 0)
01330 break;
01331 desc_list_end = p + desc_list_len;
01332 if (desc_list_end > p_end)
01333 break;
01334 for(;;) {
01335 desc_tag = get8(&p, desc_list_end);
01336 if (desc_tag < 0)
01337 break;
01338 desc_len = get8(&p, desc_list_end);
01339 desc_end = p + desc_len;
01340 if (desc_end > desc_list_end)
01341 break;
01342 #ifdef DEBUG_SI
01343 av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
01344 desc_tag, desc_len);
01345 #endif
01346 switch(desc_tag) {
01347 case 0x48:
01348 service_type = get8(&p, p_end);
01349 if (service_type < 0)
01350 break;
01351 provider_name = getstr8(&p, p_end);
01352 if (!provider_name)
01353 break;
01354 name = getstr8(&p, p_end);
01355 if (!name)
01356 break;
01357 new_service(ts, sid, provider_name, name);
01358 break;
01359 default:
01360 break;
01361 }
01362 p = desc_end;
01363 }
01364 p = desc_list_end;
01365 }
01366 ts->stop_parse = 1;
01367
01368
01369 mpegts_close_filter(ts, ts->sdt_filter);
01370 ts->sdt_filter = NULL;
01371 }
01372 #endif
01373
01374 #if 0
01375
01376 void mpegts_scan_sdt(MpegTSContext *ts)
01377 {
01378 ts->sdt_filter = mpegts_open_section_filter(ts, SDT_PID,
01379 sdt_cb, ts, 1);
01380 }
01381 #endif
01382
01383 #if 0
01384
01385
01386 void mpegts_scan_pat(MpegTSContext *ts)
01387 {
01388 ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,
01389 pat_scan_cb, ts, 1);
01390 }
01391 #endif
01392
01393 static int64_t get_pts(const uint8_t *p)
01394 {
01395 int64_t pts;
01396 int val;
01397
01398 pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
01399 val = (p[1] << 8) | p[2];
01400 pts |= (int64_t)(val >> 1) << 15;
01401 val = (p[3] << 8) | p[4];
01402 pts |= (int64_t)(val >> 1);
01403 return pts;
01404 }
01405
01406
01407 static void init_stream(AVStream *st, int stream_type, int code)
01408 {
01409 int codec_type=-1, codec_id=-1;
01410 switch(stream_type) {
01411 case STREAM_TYPE_AUDIO_MPEG1:
01412 case STREAM_TYPE_AUDIO_MPEG2:
01413 codec_type = CODEC_TYPE_AUDIO;
01414 codec_id = CODEC_ID_MP3;
01415 break;
01416 case STREAM_TYPE_VIDEO_MPEG1:
01417 case STREAM_TYPE_VIDEO_MPEG2:
01418 codec_type = CODEC_TYPE_VIDEO;
01419 codec_id = CODEC_ID_MPEG2VIDEO;
01420 break;
01421 case STREAM_TYPE_VIDEO_MPEG4:
01422 codec_type = CODEC_TYPE_VIDEO;
01423 codec_id = CODEC_ID_MPEG4;
01424 break;
01425 case STREAM_TYPE_VIDEO_H264:
01426 codec_type = CODEC_TYPE_VIDEO;
01427 codec_id = CODEC_ID_H264;
01428 break;
01429 case STREAM_TYPE_VIDEO_VC1:
01430 codec_type = CODEC_TYPE_VIDEO;
01431 codec_id = CODEC_ID_VC1;
01432 break;
01433 case STREAM_TYPE_AUDIO_AAC:
01434 codec_type = CODEC_TYPE_AUDIO;
01435 codec_id = CODEC_ID_AAC;
01436 break;
01437 case STREAM_TYPE_AUDIO_AC3:
01438 codec_type = CODEC_TYPE_AUDIO;
01439 codec_id = CODEC_ID_AC3;
01440 break;
01441 case STREAM_TYPE_AUDIO_DTS:
01442 codec_type = CODEC_TYPE_AUDIO;
01443 codec_id = CODEC_ID_DTS;
01444 break;
01445 case STREAM_TYPE_VBI_DVB:
01446 codec_type = CODEC_TYPE_DATA;
01447 codec_id = CODEC_ID_DVB_VBI;
01448 break;
01449 case STREAM_TYPE_SUBTITLE_DVB:
01450 codec_type = CODEC_TYPE_SUBTITLE;
01451 codec_id = CODEC_ID_DVB_SUBTITLE;
01452 break;
01453 case STREAM_TYPE_DSMCC_B:
01454 codec_type = CODEC_TYPE_DATA;
01455 codec_id = CODEC_ID_DSMCC_B;
01456 break;
01457 case STREAM_TYPE_PRIVATE_DATA:
01458 default:
01459 if (code >= 0x1c0 && code <= 0x1df) {
01460 codec_type = CODEC_TYPE_AUDIO;
01461 codec_id = CODEC_ID_MP2;
01462 } else if (code == 0x1bd) {
01463 codec_type = CODEC_TYPE_AUDIO;
01464 codec_id = CODEC_ID_AC3;
01465 } else {
01466 codec_type = CODEC_TYPE_VIDEO;
01467 codec_id = CODEC_ID_MPEG1VIDEO;
01468 }
01469 break;
01470 }
01471 st->codec->codec_type = codec_type;
01472 st->codec->codec_id = codec_id;
01473 av_set_pts_info(st, 33, 1, 90000);
01474 }
01475
01476 static AVStream *new_pes_av_stream(PESContext *pes, uint32_t code)
01477 {
01478 CHECKED_ALLOCZ(pes->st, sizeof(AVStream));
01479 pes->st->codec = avcodec_alloc_context();
01480 init_stream(pes->st, pes->stream_type, code);
01481 pes->st->priv_data = pes;
01482 pes->st->need_parsing = AVSTREAM_PARSE_FULL;
01483
01484 pes->st = av_add_stream(pes->stream, pes->st, pes->pid);
01485 fail:
01486 return pes->st;
01487 }
01488
01489 static AVStream *new_section_av_stream(SectionContext *sect, uint32_t code)
01490 {
01491 CHECKED_ALLOCZ(sect->st, sizeof(AVStream));
01492 sect->st->codec = avcodec_alloc_context();
01493 init_stream(sect->st, sect->stream_type, code);
01494 sect->st->priv_data = sect;
01495 sect->st->need_parsing = AVSTREAM_PARSE_NONE;
01496
01497 sect->st = av_add_stream(sect->stream, sect->st, sect->pid);
01498 fail:
01499 return sect->st;
01500 }
01501
01502
01503
01504 static void mpegts_push_data(void *opaque,
01505 const uint8_t *buf, int buf_size, int is_start,
01506 int64_t position)
01507 {
01508 PESContext *pes = opaque;
01509 MpegTSContext *ts = pes->ts;
01510 const uint8_t *p;
01511 int len, code;
01512
01513 if (is_start) {
01514 pes->startpos = position;
01515 pes->state = MPEGTS_HEADER;
01516 pes->data_index = 0;
01517 }
01518 p = buf;
01519 while (buf_size > 0) {
01520 switch(pes->state) {
01521 case MPEGTS_HEADER:
01522 len = PES_START_SIZE - pes->data_index;
01523 if (len > buf_size)
01524 len = buf_size;
01525 memcpy(pes->header + pes->data_index, p, len);
01526 pes->data_index += len;
01527 p += len;
01528 buf_size -= len;
01529 if (pes->data_index == PES_START_SIZE) {
01530
01531
01532 #if 0
01533 av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
01534 #endif
01535 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
01536 pes->header[2] == 0x01) {
01537
01538 code = pes->header[3] | 0x100;
01539 if (!((code >= 0x1c0 && code <= 0x1df) ||
01540 (code >= 0x1e0 && code <= 0x1ef) ||
01541 (code == 0x1bd) || (code == 0x1fd)))
01542 goto skip;
01543 if (!pes->st && 0 == new_pes_av_stream(pes, code)) {
01544 av_log(NULL, AV_LOG_ERROR,
01545 "Error: new_pes_av_stream() "
01546 "failed in mpegts_push_data\n");
01547 goto skip;
01548 }
01549 pes->state = MPEGTS_PESHEADER_FILL;
01550 pes->total_size = (pes->header[4] << 8) | pes->header[5];
01551
01552
01553 if (pes->total_size)
01554 pes->total_size += 6;
01555 pes->pes_header_size = pes->header[8] + 9;
01556 } else {
01557
01558
01559 skip:
01560 pes->state = MPEGTS_SKIP;
01561 continue;
01562 }
01563 }
01564 break;
01565
01566
01567 case MPEGTS_PESHEADER_FILL:
01568 len = pes->pes_header_size - pes->data_index;
01569 if (len > buf_size)
01570 len = buf_size;
01571 memcpy(pes->header + pes->data_index, p, len);
01572 pes->data_index += len;
01573 p += len;
01574 buf_size -= len;
01575 if (pes->data_index == pes->pes_header_size) {
01576 const uint8_t *r;
01577 unsigned int flags;
01578
01579 flags = pes->header[7];
01580 r = pes->header + 9;
01581 pes->pts = AV_NOPTS_VALUE;
01582 pes->dts = AV_NOPTS_VALUE;
01583 if ((flags & 0xc0) == 0x80) {
01584 pes->pts = get_pts(r);
01585 r += 5;
01586 } else if ((flags & 0xc0) == 0xc0) {
01587 pes->pts = get_pts(r);
01588 r += 5;
01589 pes->dts = get_pts(r);
01590 r += 5;
01591 }
01592
01593 pes->state = MPEGTS_PAYLOAD;
01594 }
01595 break;
01596 case MPEGTS_PAYLOAD:
01597 if (pes->total_size) {
01598 len = pes->total_size - pes->data_index;
01599 if (len > buf_size)
01600 len = buf_size;
01601 } else {
01602 len = buf_size;
01603 }
01604 if (len > 0) {
01605 AVPacket *pkt = ts->pkt;
01606 if (pkt && pes->st && av_new_packet(pkt, len) == 0) {
01607 memcpy(pkt->data, p, len);
01608 pkt->stream_index = pes->st->index;
01609 pkt->pts = pes->pts;
01610 pkt->dts = pes->dts;
01611 pkt->pos = pes->startpos;
01612
01613 pes->pts = AV_NOPTS_VALUE;
01614 pes->dts = AV_NOPTS_VALUE;
01615 ts->stop_parse = 1;
01616 return;
01617 }
01618 }
01619 buf_size = 0;
01620 break;
01621 case MPEGTS_SKIP:
01622 buf_size = 0;
01623 break;
01624 }
01625 }
01626 }
01627
01628 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int stream_type)
01629 {
01630 MpegTSFilter *tss = ts->pids[pid];
01631 PESContext *pes = 0;
01632 if (tss) {
01633 if (tss->type == MPEGTS_PES)
01634 pes = (PESContext*) tss->u.pes_filter.opaque;
01635 if (pes && (pes->stream_type == stream_type)) {
01636 return pes;
01637 }
01638
01639 mpegts_close_filter(ts, tss);
01640 }
01641
01642
01643 if (!(pes=av_mallocz(sizeof(PESContext)))) {
01644 av_log(NULL, AV_LOG_ERROR, "Error: av_mallocz() failed in add_pes_stream");
01645 return 0;
01646 }
01647 pes->ts = ts;
01648 pes->stream = ts->stream;
01649 pes->pid = pid;
01650 pes->stream_type = stream_type;
01651 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
01652 if (!tss) {
01653 av_free(pes);
01654 av_log(NULL, AV_LOG_ERROR, "Error: unable to open "
01655 "mpegts PES filter in add_pes_stream");
01656 return 0;
01657 }
01658 return pes;
01659 }
01660
01661
01662
01663
01664
01665
01666
01667 static void mpegts_push_section(void *opaque, const uint8_t *section, int section_len)
01668 {
01669 SectionContext *sect = opaque;
01670 MpegTSContext *ts = sect->ts;
01671 SectionHeader header;
01672 AVPacket *pkt = ts->pkt;
01673 const uint8_t *p = section, *p_end = section + section_len - 4;
01674 if (parse_section_header(&header, &p, p_end) < 0)
01675 {
01676 av_log(NULL, AV_LOG_DEBUG, "Unable to parse header\n");
01677 return;
01678 }
01679 if (pkt->data) {
01680 uint8_t *data = pkt->data;
01681 int space = pkt->size;
01682 int table_size = 0;
01683 while (space > 3 + table_size) {
01684 table_size = (((data[1] & 0xf) << 8) | data[2]) + 3;
01685 if (table_size < space) {
01686 space -= table_size;
01687 data += table_size;
01688 }
01689 }
01690 if (space < section_len) {
01691 av_log(NULL, AV_LOG_DEBUG, "Insufficient space for additional packet\n");
01692 return;
01693 }
01694 memcpy(data, section, section_len);
01695 } else if (pkt && sect->st) {
01696 int pktLen = section_len + 184;
01697 if (av_new_packet(pkt, pktLen) == 0) {
01698 memcpy(pkt->data, section, section_len);
01699 memset(pkt->data+section_len, 0xff, pktLen-section_len);
01700 pkt->stream_index = sect->st->index;
01701 pkt->pts = AV_NOPTS_VALUE;
01702 pkt->dts = AV_NOPTS_VALUE;
01703 pkt->pos = 0;
01704 ts->stop_parse = 1;
01705 }
01706 }
01707 }
01708
01709 static SectionContext *add_section_stream(MpegTSContext *ts, int pid, int stream_type)
01710 {
01711 MpegTSFilter *tss = ts->pids[pid];
01712 SectionContext *sect = 0;
01713 if (tss) {
01714 if (tss->type == MPEGTS_SECTION)
01715 sect = (SectionContext*) tss->u.section_filter.opaque;
01716
01717 if (sect && (sect->stream_type == stream_type))
01718 return sect;
01719
01720
01721 mpegts_close_filter(ts, tss);
01722 }
01723
01724
01725 if (!(sect=av_mallocz(sizeof(SectionContext)))) {
01726 av_log(NULL, AV_LOG_ERROR, "Error: av_mallocz() failed in add_pes_stream");
01727 return 0;
01728 }
01729 sect->ts = ts;
01730 sect->stream = ts->stream;
01731 sect->pid = pid;
01732 sect->stream_type = stream_type;
01733 tss = mpegts_open_section_filter(ts, pid, mpegts_push_section, sect, 1);
01734 if (!tss) {
01735 av_free(sect);
01736 av_log(NULL, AV_LOG_ERROR, "Error: unable to open mpegts Section filter in add_section_stream");
01737 return 0;
01738 }
01739
01740 return sect;
01741 }
01742
01743
01744
01745 static void handle_packet(MpegTSContext *ts, const uint8_t *packet,
01746 int64_t position)
01747 {
01748 AVFormatContext *s = ts->stream;
01749 MpegTSFilter *tss;
01750 int len, pid, cc, cc_ok, afc, is_start;
01751 const uint8_t *p, *p_end;
01752
01753 if (!ts->pids[0]) {
01754
01755 ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
01756 }
01757
01758 pid = ((packet[1] & 0x1f) << 8) | packet[2];
01759 is_start = packet[1] & 0x40;
01760 tss = ts->pids[pid];
01761 if (ts->auto_guess && tss == NULL && is_start) {
01762 add_pes_stream(ts, pid, 0);
01763 tss = ts->pids[pid];
01764 }
01765 if (!tss)
01766 return;
01767
01768
01769 cc = (packet[3] & 0xf);
01770 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
01771 tss->last_cc = cc;
01772
01773
01774 afc = (packet[3] >> 4) & 3;
01775 p = packet + 4;
01776 if (afc == 0)
01777 return;
01778 if (afc == 2)
01779 return;
01780 if (afc == 3) {
01781
01782 p += p[0] + 1;
01783 }
01784
01785 p_end = packet + TS_PACKET_SIZE;
01786 if (p >= p_end)
01787 return;
01788
01789 if (tss->type == MPEGTS_SECTION) {
01790 if (is_start) {
01791
01792 len = *p++;
01793 if (p + len > p_end)
01794 return;
01795 if (len && cc_ok) {
01796
01797 write_section_data(s, tss,
01798 p, len, 0);
01799
01800 if (!ts->pids[pid])
01801 return;
01802 }
01803 p += len;
01804 if (p < p_end) {
01805 write_section_data(s, tss,
01806 p, p_end - p, 1);
01807 }
01808 } else {
01809 if (cc_ok) {
01810 write_section_data(s, tss,
01811 p, p_end - p, 0);
01812 }
01813 }
01814 } else {
01815 tss->u.pes_filter.pes_cb(tss->u.pes_filter.opaque,
01816 p, p_end - p, is_start, position);
01817 }
01818 }
01819
01820
01821
01822 static int mpegts_resync(ByteIOContext *pb)
01823 {
01824 int c, i;
01825
01826 for(i = 0;i < MAX_RESYNC_SIZE; i++) {
01827 c = url_fgetc(pb);
01828 if (c < 0)
01829 return -1;
01830 if (c == 0x47) {
01831 url_fseek(pb, -1, SEEK_CUR);
01832 return 0;
01833 }
01834 }
01835
01836 return -1;
01837 }
01838
01839
01840 static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size,
01841 int64_t *position)
01842 {
01843 int skip, len;
01844
01845 for(;;) {
01846 *position = url_ftell(pb);
01847 len = get_buffer(pb, buf, TS_PACKET_SIZE);
01848 if (len != TS_PACKET_SIZE)
01849 return AVERROR(EIO);
01850
01851 if (buf[0] != 0x47) {
01852
01853 url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
01854 if (mpegts_resync(pb) < 0)
01855 return AVERROR_INVALIDDATA;
01856 else
01857 continue;
01858 } else {
01859 skip = raw_packet_size - TS_PACKET_SIZE;
01860 if (skip > 0)
01861 url_fskip(pb, skip);
01862 break;
01863 }
01864 }
01865 return 0;
01866 }
01867
01868 static int handle_packets(MpegTSContext *ts, int nb_packets)
01869 {
01870 AVFormatContext *s = ts->stream;
01871 ByteIOContext *pb = &s->pb;
01872 uint8_t packet[TS_PACKET_SIZE];
01873 int packet_num, ret;
01874 int64_t pos;
01875
01876 ts->stop_parse = 0;
01877 packet_num = 0;
01878 for(;;) {
01879 if (ts->stop_parse)
01880 break;
01881 packet_num++;
01882 if (nb_packets != 0 && packet_num >= nb_packets)
01883 break;
01884 ret = read_packet(pb, packet, ts->raw_packet_size, &pos);
01885 if (ret != 0)
01886 return ret;
01887 handle_packet(ts, packet, pos);
01888 }
01889 return 0;
01890 }
01891
01892 static int mpegts_probe(AVProbeData *p)
01893 {
01894 #if 1
01895 const int size= p->buf_size;
01896 int score, fec_score, dvhs_score;
01897 #define CHECK_COUNT 10
01898
01899 if (size < (TS_FEC_PACKET_SIZE * CHECK_COUNT))
01900 return -1;
01901
01902 score = analyze(p->buf, TS_PACKET_SIZE *CHECK_COUNT, TS_PACKET_SIZE, NULL);
01903 dvhs_score = analyze(p->buf, TS_DVHS_PACKET_SIZE * CHECK_COUNT,
01904 TS_DVHS_PACKET_SIZE, NULL);
01905 fec_score= analyze(p->buf, TS_FEC_PACKET_SIZE*CHECK_COUNT, TS_FEC_PACKET_SIZE, NULL);
01906 #if 0
01907 av_log(NULL, AV_LOG_DEBUG, "score: %d, fec_score: %d \n",
01908 score, fec_score);
01909 #endif
01910
01911
01912
01913 if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
01914 else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
01915 else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
01916 else return -1;
01917 #else
01918
01919 if (match_ext(p->filename, "ts"))
01920 return AVPROBE_SCORE_MAX;
01921 else
01922 return 0;
01923 #endif
01924 }
01925
01926 #if 0
01927 static void set_service_cb(void *opaque, int ret)
01928 {
01929 MpegTSContext *ts = opaque;
01930 ts->set_service_ret = ret;
01931 ts->stop_parse = 1;
01932 }
01933 #endif
01934
01935
01936
01937 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
01938 const uint8_t *packet)
01939 {
01940 int afc, len, flags;
01941 const uint8_t *p;
01942 unsigned int v;
01943
01944 afc = (packet[3] >> 4) & 3;
01945 if (afc <= 1)
01946 return -1;
01947 p = packet + 4;
01948 len = p[0];
01949 p++;
01950 if (len == 0)
01951 return -1;
01952 flags = *p++;
01953 len--;
01954 if (!(flags & 0x10))
01955 return -1;
01956 if (len < 6)
01957 return -1;
01958 v = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
01959 *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
01960 *ppcr_low = ((p[4] & 1) << 8) | p[5];
01961 return 0;
01962 }
01963
01964 static int mpegts_read_header(AVFormatContext *s,
01965 AVFormatParameters *ap)
01966 {
01967 MpegTSContext *ts = s->priv_data;
01968 ByteIOContext *pb = &s->pb;
01969 uint8_t buf[1024];
01970 int len, sid, i;
01971 int64_t pos;
01972 MpegTSService *service;
01973
01974 if (ap) {
01975 ts->mpeg2ts_raw = ap->mpeg2ts_raw;
01976 ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
01977 }
01978
01979
01980 pos = url_ftell(pb);
01981 len = get_buffer(pb, buf, sizeof(buf));
01982 if (len != sizeof(buf)) {
01983 av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: "
01984 "unable to read first 1024 bytes\n");
01985 goto fail;
01986 }
01987 ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
01988 if (ts->raw_packet_size <= 0) {
01989 av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: "
01990 "packet size incorrect\n");
01991 goto fail;
01992 }
01993 ts->stream = s;
01994 ts->auto_guess = 0;
01995
01996 if (!ts->mpeg2ts_raw) {
01997
01998
01999 if (!ts->auto_guess) {
02000
02001
02002
02003
02004 ts->req_sid = -1;
02005
02006 ts->scanning = 1;
02007 ts->pat_filter = mpegts_open_section_filter(
02008 ts, PAT_PID, pat_cb, ts, 1);
02009 url_fseek(pb, pos, SEEK_SET);
02010 handle_packets(ts, s->probesize);
02011 ts->scanning = 0;
02012
02013 if (ts->nb_services <= 0) {
02014
02015 ts->auto_guess = 1;
02016 s->ctx_flags |= AVFMTCTX_NOHEADER;
02017 goto do_pcr;
02018 }
02019
02020 ts->scanning = 1;
02021 ts->pmt_scan_state = PMT_NOT_YET_FOUND;
02022
02023 for (i = 0; ((i < ts->nb_services) &&
02024 (ts->pmt_scan_state == PMT_NOT_YET_FOUND)); i++)
02025 {
02026 service = ts->services[i];
02027 #ifdef DEBUG_SI
02028 av_log(ts->stream, AV_LOG_DEBUG, "Tuning to '%s' pnum: 0x%x\n",
02029 service->name, service->sid);
02030 #endif
02031
02032
02033
02034
02035 url_fseek(pb, pos, SEEK_SET);
02036 ts->req_sid = sid = service->sid;
02037 handle_packets(ts, s->probesize);
02038
02039
02040
02041 if (ts->pmt_filter &&
02042 (ts->pmt_scan_state == PMT_NOT_YET_FOUND))
02043 {
02044 av_log(NULL, AV_LOG_ERROR,
02045 "Tuning to '%s' pnum: 0x%x "
02046 "without CRC check on PMT\n",
02047 service->name, service->sid);
02048
02049 ts->pmt_filter->u.section_filter.check_crc = 0;
02050
02051 url_fseek(pb, pos, SEEK_SET);
02052 ts->req_sid = sid = service->sid;
02053 handle_packets(ts, s->probesize);
02054 }
02055 }
02056 ts->scanning = 0;
02057
02058
02059 if (ts->pmt_scan_state == PMT_NOT_YET_FOUND)
02060 {
02061 av_log(NULL, AV_LOG_ERROR,
02062 "mpegts_read_header: could not find any PMT's\n");
02063 goto fail;
02064 }
02065
02066 #ifdef DEBUG_SI
02067 av_log(ts->stream, AV_LOG_DEBUG, "tuning done\n");
02068 #endif
02069 }
02070 s->ctx_flags |= AVFMTCTX_NOHEADER;
02071 } else {
02072 AVStream *st;
02073 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
02074 int64_t pcrs[2], pcr_h, position;
02075 int packet_count[2];
02076 uint8_t packet[TS_PACKET_SIZE];
02077
02078
02079
02080 do_pcr:
02081 st = av_new_stream(s, 0);
02082 if (!st) {
02083 av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: "
02084 "av_new_stream() failed\n");
02085 goto fail;
02086 }
02087 av_set_pts_info(st, 33, 1, 27000000);
02088 st->codec->codec_type = CODEC_TYPE_DATA;
02089 st->codec->codec_id = CODEC_ID_MPEG2TS;
02090
02091
02092 pcr_pid = -1;
02093 nb_pcrs = 0;
02094 nb_packets = 0;
02095 for(;;) {
02096 ret = read_packet(&s->pb, packet, ts->raw_packet_size, &position);
02097 if (ret < 0) {
02098 av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: "
02099 "read_packet() failed\n");
02100 goto fail;
02101 }
02102 pid = ((packet[1] & 0x1f) << 8) | packet[2];
02103 if ((pcr_pid == -1 || pcr_pid == pid) &&
02104 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
02105 pcr_pid = pid;
02106 packet_count[nb_pcrs] = nb_packets;
02107 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
02108 nb_pcrs++;
02109 if (nb_pcrs >= 2)
02110 break;
02111 }
02112 nb_packets++;
02113 }
02114 ts->pcr_pid = pcr_pid;
02115
02116
02117
02118 ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
02119 ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
02120 s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
02121 st->codec->bit_rate = s->bit_rate;
02122 st->start_time = ts->cur_pcr;
02123 #if 0
02124 av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
02125 st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
02126 #endif
02127 }
02128
02129 url_fseek(pb, pos, SEEK_SET);
02130 return 0;
02131 fail:
02132 return -1;
02133 }
02134
02135 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
02136
02137 static int mpegts_raw_read_packet(AVFormatContext *s,
02138 AVPacket *pkt)
02139 {
02140 MpegTSContext *ts = s->priv_data;
02141 int ret, i;
02142 int64_t pcr_h, next_pcr_h, pos, position;
02143 int pcr_l, next_pcr_l;
02144 uint8_t pcr_buf[12];
02145
02146 if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
02147 return AVERROR(ENOMEM);
02148 ret = read_packet(&s->pb, pkt->data, ts->raw_packet_size, &position);
02149 if (ret < 0) {
02150 av_free_packet(pkt);
02151 return ret;
02152 }
02153 if (ts->mpeg2ts_compute_pcr) {
02154
02155 if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
02156
02157 pos = url_ftell(&s->pb);
02158 for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
02159 url_fseek(&s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
02160 get_buffer(&s->pb, pcr_buf, 12);
02161 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
02162
02163 ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
02164 (i + 1);
02165 break;
02166 }
02167 }
02168 url_fseek(&s->pb, pos, SEEK_SET);
02169
02170 ts->cur_pcr = pcr_h * 300 + pcr_l;
02171 }
02172 pkt->pts = ts->cur_pcr;
02173 pkt->duration = ts->pcr_incr;
02174 ts->cur_pcr += ts->pcr_incr;
02175 }
02176 pkt->stream_index = 0;
02177 return 0;
02178 }
02179
02180 static int mpegts_read_packet(AVFormatContext *s,
02181 AVPacket *pkt)
02182 {
02183 MpegTSContext *ts = s->priv_data;
02184
02185 if (!ts->mpeg2ts_raw) {
02186 ts->pkt = pkt;
02187 return handle_packets(ts, 0);
02188 } else {
02189 return mpegts_raw_read_packet(s, pkt);
02190 }
02191 }
02192
02193 static int mpegts_read_close(AVFormatContext *s)
02194 {
02195 MpegTSContext *ts = s->priv_data;
02196 int i;
02197 for(i=0;i<NB_PID_MAX;i++)
02198 if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
02199
02200 for(i = 0; i < ts->nb_services; i++){
02201 av_free(ts->services[i]->provider_name);
02202 av_free(ts->services[i]->name);
02203 av_free(ts->services[i]);
02204 }
02205 av_freep(&ts->services);
02206
02207 return 0;
02208 }
02209
02210 static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
02211 int64_t *ppos, int64_t pos_limit)
02212 {
02213 MpegTSContext *ts = s->priv_data;
02214 int64_t pos, timestamp;
02215 uint8_t buf[TS_PACKET_SIZE];
02216 int pcr_l, pid;
02217 const int find_next= 1;
02218 pos = ((*ppos + ts->raw_packet_size - 1) / ts->raw_packet_size) * ts->raw_packet_size;
02219 if (find_next) {
02220 for(;;) {
02221 url_fseek(&s->pb, pos, SEEK_SET);
02222 if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
02223 return AV_NOPTS_VALUE;
02224 pid = ((buf[1] & 0x1f) << 8) | buf[2];
02225 if (pid == ts->pcr_pid &&
02226 parse_pcr(×tamp, &pcr_l, buf) == 0) {
02227 break;
02228 }
02229 pos += ts->raw_packet_size;
02230 }
02231 } else {
02232 for(;;) {
02233 pos -= ts->raw_packet_size;
02234 if (pos < 0)
02235 return AV_NOPTS_VALUE;
02236 url_fseek(&s->pb, pos, SEEK_SET);
02237 if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
02238 return AV_NOPTS_VALUE;
02239 pid = ((buf[1] & 0x1f) << 8) | buf[2];
02240 if (pid == ts->pcr_pid &&
02241 parse_pcr(×tamp, &pcr_l, buf) == 0) {
02242 break;
02243 }
02244 }
02245 }
02246 *ppos = pos;
02247
02248 return timestamp;
02249 }
02250
02251 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
02252 MpegTSContext *ts = s->priv_data;
02253 uint8_t buf[TS_PACKET_SIZE];
02254 int64_t pos;
02255
02256 if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
02257 return -1;
02258
02259 pos= url_ftell(&s->pb);
02260
02261 for(;;) {
02262 url_fseek(&s->pb, pos, SEEK_SET);
02263 if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
02264 return -1;
02265
02266 if(buf[1] & 0x40) break;
02267 pos += ts->raw_packet_size;
02268 }
02269 url_fseek(&s->pb, pos, SEEK_SET);
02270
02271 return 0;
02272 }
02273
02274
02275
02276
02277 MpegTSContext *mpegts_parse_open(AVFormatContext *s)
02278 {
02279 MpegTSContext *ts;
02280
02281 ts = av_mallocz(sizeof(MpegTSContext));
02282 if (!ts)
02283 return NULL;
02284
02285 ts->raw_packet_size = TS_PACKET_SIZE;
02286 ts->stream = s;
02287 ts->auto_guess = 1;
02288 return ts;
02289 }
02290
02291
02292
02293 int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
02294 const uint8_t *buf, int len)
02295 {
02296 int len1;
02297 int64_t position = 0;
02298
02299 len1 = len;
02300 ts->pkt = pkt;
02301 ts->stop_parse = 0;
02302 for(;;) {
02303 if (ts->stop_parse)
02304 break;
02305 if (len < TS_PACKET_SIZE)
02306 return -1;
02307 if (buf[0] != 0x47) {
02308 buf++;
02309 len--;
02310 } else {
02311 handle_packet(ts, buf, position);
02312 buf += TS_PACKET_SIZE;
02313 len -= TS_PACKET_SIZE;
02314 }
02315 }
02316 return len1 - len;
02317 }
02318
02319 void mpegts_parse_close(MpegTSContext *ts)
02320 {
02321 int i;
02322
02323 for(i=0;i<NB_PID_MAX;i++)
02324 av_free(ts->pids[i]);
02325 av_free(ts);
02326 }
02327
02328 AVInputFormat mpegts_demuxer = {
02329 "mpegts",
02330 "MPEG2 transport stream format",
02331 sizeof(MpegTSContext),
02332 mpegts_probe,
02333 mpegts_read_header,
02334 mpegts_read_packet,
02335 mpegts_read_close,
02336 read_seek,
02337 mpegts_get_pcr,
02338 .flags = AVFMT_SHOW_IDS,
02339 };