00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/intreadwrite.h"
00029 #include "libavutil/intfloat.h"
00030 #include "avformat.h"
00031 #include "internal.h"
00032 #include "wtv.h"
00033 #include "mpegts.h"
00034
00035
00036 #define PRI_PRETTY_GUID \
00037 "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x"
00038 #define ARG_PRETTY_GUID(g) \
00039 AV_RL32(g),AV_RL16(g+4),AV_RL16(g+6),g[8],g[9],g[10],g[11],g[12],g[13],g[14],g[15]
00040 #define LEN_PRETTY_GUID 34
00041
00042
00043
00044
00045
00046
00047
00048 typedef struct {
00049 AVIOContext *pb_filesystem;
00051 int sector_bits;
00052 uint32_t *sectors;
00053 int nb_sectors;
00055 int error;
00056 int64_t position;
00057 int64_t length;
00058 } WtvFile;
00059
00063 static int wtvfile_read_packet(void *opaque, uint8_t *buf, int buf_size)
00064 {
00065 WtvFile *wf = opaque;
00066 AVIOContext *pb = wf->pb_filesystem;
00067 int nread = 0;
00068
00069 if (wf->error || pb->error)
00070 return -1;
00071 if (wf->position >= wf->length || url_feof(pb))
00072 return 0;
00073
00074 buf_size = FFMIN(buf_size, wf->length - wf->position);
00075 while(nread < buf_size) {
00076 int n;
00077 int remaining_in_sector = (1 << wf->sector_bits) - (wf->position & ((1 << wf->sector_bits) - 1));
00078 int read_request = FFMIN(buf_size - nread, remaining_in_sector);
00079
00080 n = avio_read(pb, buf, read_request);
00081 if (n <= 0)
00082 break;
00083 nread += n;
00084 buf += n;
00085 wf->position += n;
00086 if (n == remaining_in_sector) {
00087 int i = wf->position >> wf->sector_bits;
00088 if (i >= wf->nb_sectors ||
00089 (wf->sectors[i] != wf->sectors[i - 1] + (1 << (wf->sector_bits - WTV_SECTOR_BITS)) &&
00090 avio_seek(pb, (int64_t)wf->sectors[i] << WTV_SECTOR_BITS, SEEK_SET) < 0)) {
00091 wf->error = 1;
00092 break;
00093 }
00094 }
00095 }
00096 return nread;
00097 }
00098
00102 static int64_t wtvfile_seek(void *opaque, int64_t offset, int whence)
00103 {
00104 WtvFile *wf = opaque;
00105 AVIOContext *pb = wf->pb_filesystem;
00106
00107 if (whence == AVSEEK_SIZE)
00108 return wf->length;
00109 else if (whence == SEEK_CUR)
00110 offset = wf->position + offset;
00111 else if (whence == SEEK_END)
00112 offset = wf->length;
00113
00114 wf->error = offset < 0 || offset >= wf->length ||
00115 avio_seek(pb, ((int64_t)wf->sectors[offset >> wf->sector_bits] << WTV_SECTOR_BITS)
00116 + (offset & ((1 << wf->sector_bits) - 1)), SEEK_SET) < 0;
00117 wf->position = offset;
00118 return offset;
00119 }
00120
00128 static int read_ints(AVIOContext *pb, uint32_t *data, int count)
00129 {
00130 int i, total = 0;
00131 for (i = 0; i < count; i++) {
00132 if ((data[total] = avio_rl32(pb)))
00133 total++;
00134 }
00135 return total;
00136 }
00137
00145 static AVIOContext * wtvfile_open_sector(int first_sector, uint64_t length, int depth, AVFormatContext *s)
00146 {
00147 AVIOContext *pb;
00148 WtvFile *wf;
00149 uint8_t *buffer;
00150
00151 if (avio_seek(s->pb, first_sector << WTV_SECTOR_BITS, SEEK_SET) < 0)
00152 return NULL;
00153
00154 wf = av_mallocz(sizeof(WtvFile));
00155 if (!wf)
00156 return NULL;
00157
00158 if (depth == 0) {
00159 wf->sectors = av_malloc(sizeof(uint32_t));
00160 if (!wf->sectors) {
00161 av_free(wf);
00162 return NULL;
00163 }
00164 wf->sectors[0] = first_sector;
00165 wf->nb_sectors = 1;
00166 } else if (depth == 1) {
00167 wf->sectors = av_malloc(WTV_SECTOR_SIZE);
00168 if (!wf->sectors) {
00169 av_free(wf);
00170 return NULL;
00171 }
00172 wf->nb_sectors = read_ints(s->pb, wf->sectors, WTV_SECTOR_SIZE / 4);
00173 } else if (depth == 2) {
00174 uint32_t sectors1[WTV_SECTOR_SIZE / 4];
00175 int nb_sectors1 = read_ints(s->pb, sectors1, WTV_SECTOR_SIZE / 4);
00176 int i;
00177
00178 wf->sectors = av_malloc(nb_sectors1 << WTV_SECTOR_BITS);
00179 if (!wf->sectors) {
00180 av_free(wf);
00181 return NULL;
00182 }
00183 wf->nb_sectors = 0;
00184 for (i = 0; i < nb_sectors1; i++) {
00185 if (avio_seek(s->pb, (int64_t)sectors1[i] << WTV_SECTOR_BITS, SEEK_SET) < 0)
00186 break;
00187 wf->nb_sectors += read_ints(s->pb, wf->sectors + i * WTV_SECTOR_SIZE / 4, WTV_SECTOR_SIZE / 4);
00188 }
00189 } else {
00190 av_log(s, AV_LOG_ERROR, "unsupported file allocation table depth (0x%x)\n", depth);
00191 av_free(wf);
00192 return NULL;
00193 }
00194 wf->sector_bits = length & (1ULL<<63) ? WTV_SECTOR_BITS : WTV_BIGSECTOR_BITS;
00195
00196 if (!wf->nb_sectors) {
00197 av_free(wf->sectors);
00198 av_free(wf);
00199 return NULL;
00200 }
00201
00202
00203 length &= 0xFFFFFFFFFFFF;
00204 if (length > ((int64_t)wf->nb_sectors << wf->sector_bits)) {
00205 av_log(s, AV_LOG_WARNING, "reported file length (0x%"PRIx64") exceeds number of available sectors (0x%"PRIx64")\n", length, (int64_t)wf->nb_sectors << wf->sector_bits);
00206 length = (int64_t)wf->nb_sectors << wf->sector_bits;
00207 }
00208 wf->length = length;
00209
00210
00211 wf->position = 0;
00212 if (avio_seek(s->pb, (int64_t)wf->sectors[0] << WTV_SECTOR_BITS, SEEK_SET) < 0) {
00213 av_free(wf->sectors);
00214 av_free(wf);
00215 return NULL;
00216 }
00217
00218 wf->pb_filesystem = s->pb;
00219 buffer = av_malloc(1 << wf->sector_bits);
00220 if (!buffer) {
00221 av_free(wf->sectors);
00222 av_free(wf);
00223 return NULL;
00224 }
00225
00226 pb = avio_alloc_context(buffer, 1 << wf->sector_bits, 0, wf,
00227 wtvfile_read_packet, NULL, wtvfile_seek);
00228 if (!pb) {
00229 av_free(buffer);
00230 av_free(wf->sectors);
00231 av_free(wf);
00232 }
00233 return pb;
00234 }
00235
00244 static AVIOContext * wtvfile_open2(AVFormatContext *s, const uint8_t *buf, int buf_size, const uint8_t *filename, int filename_size)
00245 {
00246 const uint8_t *buf_end = buf + buf_size;
00247
00248 while(buf + 48 <= buf_end) {
00249 int dir_length, name_size, first_sector, depth;
00250 uint64_t file_length;
00251 const uint8_t *name;
00252 if (ff_guidcmp(buf, ff_dir_entry_guid)) {
00253 av_log(s, AV_LOG_ERROR, "unknown guid "FF_PRI_GUID", expected dir_entry_guid; "
00254 "remaining directory entries ignored\n", FF_ARG_GUID(buf));
00255 break;
00256 }
00257 dir_length = AV_RL16(buf + 16);
00258 file_length = AV_RL64(buf + 24);
00259 name_size = 2 * AV_RL32(buf + 32);
00260 if (buf + 48 + (int64_t)name_size > buf_end || name_size<0) {
00261 av_log(s, AV_LOG_ERROR, "filename exceeds buffer size; remaining directory entries ignored\n");
00262 break;
00263 }
00264 first_sector = AV_RL32(buf + 40 + name_size);
00265 depth = AV_RL32(buf + 44 + name_size);
00266
00267
00268 name = buf + 40;
00269 if (name_size >= filename_size &&
00270 !memcmp(name, filename, filename_size) &&
00271 (name_size < filename_size + 2 || !AV_RN16(name + filename_size)))
00272 return wtvfile_open_sector(first_sector, file_length, depth, s);
00273
00274 buf += dir_length;
00275 }
00276 return 0;
00277 }
00278
00279 #define wtvfile_open(s, buf, buf_size, filename) \
00280 wtvfile_open2(s, buf, buf_size, filename, sizeof(filename))
00281
00285 static void wtvfile_close(AVIOContext *pb)
00286 {
00287 WtvFile *wf = pb->opaque;
00288 av_free(wf->sectors);
00289 av_freep(&pb->opaque);
00290 av_freep(&pb->buffer);
00291 av_free(pb);
00292 }
00293
00294
00295
00296
00297
00298
00299
00300 typedef struct {
00301 int seen_data;
00302 } WtvStream;
00303
00304 typedef struct {
00305 AVIOContext *pb;
00306 int64_t epoch;
00307 int64_t pts;
00308 int64_t last_valid_pts;
00310
00311
00312 AVIndexEntry *index_entries;
00313 int nb_index_entries;
00314 unsigned int index_entries_allocated_size;
00315 } WtvContext;
00316
00317
00318 static const ff_asf_guid EVENTID_SubtitleSpanningEvent =
00319 {0x48,0xC0,0xCE,0x5D,0xB9,0xD0,0x63,0x41,0x87,0x2C,0x4F,0x32,0x22,0x3B,0xE8,0x8A};
00320 static const ff_asf_guid EVENTID_LanguageSpanningEvent =
00321 {0x6D,0x66,0x92,0xE2,0x02,0x9C,0x8D,0x44,0xAA,0x8D,0x78,0x1A,0x93,0xFD,0xC3,0x95};
00322 static const ff_asf_guid EVENTID_AudioDescriptorSpanningEvent =
00323 {0x1C,0xD4,0x7B,0x10,0xDA,0xA6,0x91,0x46,0x83,0x69,0x11,0xB2,0xCD,0xAA,0x28,0x8E};
00324 static const ff_asf_guid EVENTID_CtxADescriptorSpanningEvent =
00325 {0xE6,0xA2,0xB4,0x3A,0x47,0x42,0x34,0x4B,0x89,0x6C,0x30,0xAF,0xA5,0xD2,0x1C,0x24};
00326 static const ff_asf_guid EVENTID_CSDescriptorSpanningEvent =
00327 {0xD9,0x79,0xE7,0xEf,0xF0,0x97,0x86,0x47,0x80,0x0D,0x95,0xCF,0x50,0x5D,0xDC,0x66};
00328 static const ff_asf_guid EVENTID_DVBScramblingControlSpanningEvent =
00329 {0xC4,0xE1,0xD4,0x4B,0xA1,0x90,0x09,0x41,0x82,0x36,0x27,0xF0,0x0E,0x7D,0xCC,0x5B};
00330 static const ff_asf_guid EVENTID_StreamIDSpanningEvent =
00331 {0x68,0xAB,0xF1,0xCA,0x53,0xE1,0x41,0x4D,0xA6,0xB3,0xA7,0xC9,0x98,0xDB,0x75,0xEE};
00332 static const ff_asf_guid EVENTID_TeletextSpanningEvent =
00333 {0x50,0xD9,0x99,0x95,0x33,0x5F,0x17,0x46,0xAF,0x7C,0x1E,0x54,0xB5,0x10,0xDA,0xA3};
00334 static const ff_asf_guid EVENTID_AudioTypeSpanningEvent =
00335 {0xBE,0xBF,0x1C,0x50,0x49,0xB8,0xCE,0x42,0x9B,0xE9,0x3D,0xB8,0x69,0xFB,0x82,0xB3};
00336
00337
00338
00339
00340 static const ff_asf_guid mediasubtype_mpeg1payload =
00341 {0x81,0xEB,0x36,0xE4,0x4F,0x52,0xCE,0x11,0x9F,0x53,0x00,0x20,0xAF,0x0B,0xA7,0x70};
00342 static const ff_asf_guid mediatype_mpeg2_sections =
00343 {0x6C,0x17,0x5F,0x45,0x06,0x4B,0xCE,0x47,0x9A,0xEF,0x8C,0xAE,0xF7,0x3D,0xF7,0xB5};
00344 static const ff_asf_guid mediatype_mpeg2_pes =
00345 {0x20,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA};
00346 static const ff_asf_guid mediatype_mstvcaption =
00347 {0x89,0x8A,0x8B,0xB8,0x49,0xB0,0x80,0x4C,0xAD,0xCF,0x58,0x98,0x98,0x5E,0x22,0xC1};
00348
00349
00350 static const ff_asf_guid mediasubtype_dvb_subtitle =
00351 {0xC3,0xCB,0xFF,0x34,0xB3,0xD5,0x71,0x41,0x90,0x02,0xD4,0xC6,0x03,0x01,0x69,0x7F};
00352 static const ff_asf_guid mediasubtype_teletext =
00353 {0xE3,0x76,0x2A,0xF7,0x0A,0xEB,0xD0,0x11,0xAC,0xE4,0x00,0x00,0xC0,0xCC,0x16,0xBA};
00354 static const ff_asf_guid mediasubtype_dtvccdata =
00355 {0xAA,0xDD,0x2A,0xF5,0xF0,0x36,0xF5,0x43,0x95,0xEA,0x6D,0x86,0x64,0x84,0x26,0x2A};
00356 static const ff_asf_guid mediasubtype_mpeg2_sections =
00357 {0x79,0x85,0x9F,0x4A,0xF8,0x6B,0x92,0x43,0x8A,0x6D,0xD2,0xDD,0x09,0xFA,0x78,0x61};
00358
00359
00360 static const ff_asf_guid format_videoinfo2 =
00361 {0xA0,0x76,0x2A,0xF7,0x0A,0xEB,0xD0,0x11,0xAC,0xE4,0x00,0x00,0xC0,0xCC,0x16,0xBA};
00362
00363 static int read_probe(AVProbeData *p)
00364 {
00365 return ff_guidcmp(p->buf, ff_wtv_guid) ? 0 : AVPROBE_SCORE_MAX;
00366 }
00367
00372 static int filetime_to_iso8601(char *buf, int buf_size, int64_t value)
00373 {
00374 time_t t = (value / 10000000LL) - 11644473600LL;
00375 struct tm *tm = gmtime(&t);
00376 if (!tm)
00377 return -1;
00378 strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S", gmtime(&t));
00379 return 0;
00380 }
00381
00386 static int crazytime_to_iso8601(char *buf, int buf_size, int64_t value)
00387 {
00388 time_t t = (value / 10000000LL) - 719162LL*86400LL;
00389 struct tm *tm = gmtime(&t);
00390 if (!tm)
00391 return -1;
00392 strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S", gmtime(&t));
00393 return 0;
00394 }
00395
00400 static int oledate_to_iso8601(char *buf, int buf_size, int64_t value)
00401 {
00402 time_t t = (av_int2double(value) - 25569.0) * 86400;
00403 struct tm *result= gmtime(&t);
00404 if (!result)
00405 return -1;
00406 strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S", result);
00407 return 0;
00408 }
00409
00410 static void get_attachment(AVFormatContext *s, AVIOContext *pb, int length)
00411 {
00412 char mime[1024];
00413 char description[1024];
00414 unsigned int filesize;
00415 AVStream *st;
00416 int64_t pos = avio_tell(pb);
00417
00418 avio_get_str16le(pb, INT_MAX, mime, sizeof(mime));
00419 if (strcmp(mime, "image/jpeg"))
00420 goto done;
00421
00422 avio_r8(pb);
00423 avio_get_str16le(pb, INT_MAX, description, sizeof(description));
00424 filesize = avio_rl32(pb);
00425 if (!filesize)
00426 goto done;
00427
00428 st = avformat_new_stream(s, NULL);
00429 if (!st)
00430 goto done;
00431 av_dict_set(&st->metadata, "title", description, 0);
00432 st->codec->codec_id = AV_CODEC_ID_MJPEG;
00433 st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
00434 st->codec->extradata = av_mallocz(filesize);
00435 if (!st->codec->extradata)
00436 goto done;
00437 st->codec->extradata_size = filesize;
00438 avio_read(pb, st->codec->extradata, filesize);
00439 done:
00440 avio_seek(pb, pos + length, SEEK_SET);
00441 }
00442
00443 static void get_tag(AVFormatContext *s, AVIOContext *pb, const char *key, int type, int length)
00444 {
00445 int buf_size = FFMAX(2*length, LEN_PRETTY_GUID) + 1;
00446 char *buf = av_malloc(buf_size);
00447 if (!buf)
00448 return;
00449
00450 if (type == 0 && length == 4) {
00451 snprintf(buf, buf_size, "%"PRIi32, avio_rl32(pb));
00452 } else if (type == 1) {
00453 avio_get_str16le(pb, length, buf, buf_size);
00454 if (!strlen(buf)) {
00455 av_free(buf);
00456 return;
00457 }
00458 } else if (type == 3 && length == 4) {
00459 strcpy(buf, avio_rl32(pb) ? "true" : "false");
00460 } else if (type == 4 && length == 8) {
00461 int64_t num = avio_rl64(pb);
00462 if (!strcmp(key, "WM/EncodingTime") ||
00463 !strcmp(key, "WM/MediaOriginalBroadcastDateTime")) {
00464 if (filetime_to_iso8601(buf, buf_size, num) < 0) {
00465 av_free(buf);
00466 return;
00467 }
00468 } else if (!strcmp(key, "WM/WMRVEncodeTime") ||
00469 !strcmp(key, "WM/WMRVEndTime")) {
00470 if (crazytime_to_iso8601(buf, buf_size, num) < 0) {
00471 av_free(buf);
00472 return;
00473 }
00474 } else if (!strcmp(key, "WM/WMRVExpirationDate")) {
00475 if (oledate_to_iso8601(buf, buf_size, num) < 0 ) {
00476 av_free(buf);
00477 return;
00478 }
00479 } else if (!strcmp(key, "WM/WMRVBitrate"))
00480 snprintf(buf, buf_size, "%f", av_int2double(num));
00481 else
00482 snprintf(buf, buf_size, "%"PRIi64, num);
00483 } else if (type == 5 && length == 2) {
00484 snprintf(buf, buf_size, "%"PRIi16, avio_rl16(pb));
00485 } else if (type == 6 && length == 16) {
00486 ff_asf_guid guid;
00487 avio_read(pb, guid, 16);
00488 snprintf(buf, buf_size, PRI_PRETTY_GUID, ARG_PRETTY_GUID(guid));
00489 } else if (type == 2 && !strcmp(key, "WM/Picture")) {
00490 get_attachment(s, pb, length);
00491 av_freep(&buf);
00492 return;
00493 } else {
00494 av_freep(&buf);
00495 av_log(s, AV_LOG_WARNING, "unsupported metadata entry; key:%s, type:%d, length:0x%x\n", key, type, length);
00496 avio_skip(pb, length);
00497 return;
00498 }
00499
00500 av_dict_set(&s->metadata, key, buf, 0);
00501 av_freep(&buf);
00502 }
00503
00507 static void parse_legacy_attrib(AVFormatContext *s, AVIOContext *pb)
00508 {
00509 ff_asf_guid guid;
00510 int length, type;
00511 while(!url_feof(pb)) {
00512 char key[1024];
00513 ff_get_guid(pb, &guid);
00514 type = avio_rl32(pb);
00515 length = avio_rl32(pb);
00516 if (!length)
00517 break;
00518 if (ff_guidcmp(&guid, ff_metadata_guid)) {
00519 av_log(s, AV_LOG_WARNING, "unknown guid "FF_PRI_GUID", expected metadata_guid; "
00520 "remaining metadata entries ignored\n", FF_ARG_GUID(guid));
00521 break;
00522 }
00523 avio_get_str16le(pb, INT_MAX, key, sizeof(key));
00524 get_tag(s, pb, key, type, length);
00525 }
00526
00527 ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
00528 }
00529
00534 static int parse_videoinfoheader2(AVFormatContext *s, AVStream *st)
00535 {
00536 WtvContext *wtv = s->priv_data;
00537 AVIOContext *pb = wtv->pb;
00538
00539 avio_skip(pb, 72);
00540 ff_get_bmp_header(pb, st, NULL);
00541
00542 return 72 + 40;
00543 }
00544
00548 static void parse_mpeg1waveformatex(AVStream *st)
00549 {
00550
00551 switch (AV_RL16(st->codec->extradata)) {
00552 case 0x0001 : st->codec->codec_id = AV_CODEC_ID_MP1; break;
00553 case 0x0002 : st->codec->codec_id = AV_CODEC_ID_MP2; break;
00554 case 0x0004 : st->codec->codec_id = AV_CODEC_ID_MP3; break;
00555 }
00556
00557 st->codec->bit_rate = AV_RL32(st->codec->extradata + 2);
00558
00559
00560 switch (AV_RL16(st->codec->extradata + 6)) {
00561 case 1 : case 2 : case 4 : st->codec->channels = 2; break;
00562 case 8 : st->codec->channels = 1; break;
00563 }
00564 }
00565
00571 static AVStream * new_stream(AVFormatContext *s, AVStream *st, int sid, int codec_type)
00572 {
00573 if (st) {
00574 if (st->codec->extradata) {
00575 av_freep(&st->codec->extradata);
00576 st->codec->extradata_size = 0;
00577 }
00578 } else {
00579 WtvStream *wst = av_mallocz(sizeof(WtvStream));
00580 if (!wst)
00581 return NULL;
00582 st = avformat_new_stream(s, NULL);
00583 if (!st)
00584 return NULL;
00585 st->id = sid;
00586 st->priv_data = wst;
00587 }
00588 st->codec->codec_type = codec_type;
00589 st->need_parsing = AVSTREAM_PARSE_FULL;
00590 avpriv_set_pts_info(st, 64, 1, 10000000);
00591 return st;
00592 }
00593
00603 static AVStream * parse_media_type(AVFormatContext *s, AVStream *st, int sid,
00604 ff_asf_guid mediatype, ff_asf_guid subtype,
00605 ff_asf_guid formattype, int size)
00606 {
00607 WtvContext *wtv = s->priv_data;
00608 AVIOContext *pb = wtv->pb;
00609 if (!ff_guidcmp(subtype, ff_mediasubtype_cpfilters_processed) &&
00610 !ff_guidcmp(formattype, ff_format_cpfilters_processed)) {
00611 ff_asf_guid actual_subtype;
00612 ff_asf_guid actual_formattype;
00613
00614 if (size < 32) {
00615 av_log(s, AV_LOG_WARNING, "format buffer size underflow\n");
00616 avio_skip(pb, size);
00617 return NULL;
00618 }
00619
00620 avio_skip(pb, size - 32);
00621 ff_get_guid(pb, &actual_subtype);
00622 ff_get_guid(pb, &actual_formattype);
00623 avio_seek(pb, -size, SEEK_CUR);
00624
00625 st = parse_media_type(s, st, sid, mediatype, actual_subtype, actual_formattype, size - 32);
00626 avio_skip(pb, 32);
00627 return st;
00628 } else if (!ff_guidcmp(mediatype, ff_mediatype_audio)) {
00629 st = new_stream(s, st, sid, AVMEDIA_TYPE_AUDIO);
00630 if (!st)
00631 return NULL;
00632 if (!ff_guidcmp(formattype, ff_format_waveformatex)) {
00633 int ret = ff_get_wav_header(pb, st->codec, size);
00634 if (ret < 0)
00635 return NULL;
00636 } else {
00637 if (ff_guidcmp(formattype, ff_format_none))
00638 av_log(s, AV_LOG_WARNING, "unknown formattype:"FF_PRI_GUID"\n", FF_ARG_GUID(formattype));
00639 avio_skip(pb, size);
00640 }
00641
00642 if (!memcmp(subtype + 4, (const uint8_t[]){FF_MEDIASUBTYPE_BASE_GUID}, 12)) {
00643 st->codec->codec_id = ff_wav_codec_get_id(AV_RL32(subtype), st->codec->bits_per_coded_sample);
00644 } else if (!ff_guidcmp(subtype, mediasubtype_mpeg1payload)) {
00645 if (st->codec->extradata && st->codec->extradata_size >= 22)
00646 parse_mpeg1waveformatex(st);
00647 else
00648 av_log(s, AV_LOG_WARNING, "MPEG1WAVEFORMATEX underflow\n");
00649 } else {
00650 st->codec->codec_id = ff_codec_guid_get_id(ff_codec_wav_guids, subtype);
00651 if (st->codec->codec_id == AV_CODEC_ID_NONE)
00652 av_log(s, AV_LOG_WARNING, "unknown subtype:"FF_PRI_GUID"\n", FF_ARG_GUID(subtype));
00653 }
00654 return st;
00655 } else if (!ff_guidcmp(mediatype, ff_mediatype_video)) {
00656 st = new_stream(s, st, sid, AVMEDIA_TYPE_VIDEO);
00657 if (!st)
00658 return NULL;
00659 if (!ff_guidcmp(formattype, format_videoinfo2)) {
00660 int consumed = parse_videoinfoheader2(s, st);
00661 avio_skip(pb, FFMAX(size - consumed, 0));
00662 } else if (!ff_guidcmp(formattype, ff_format_mpeg2_video)) {
00663 int consumed = parse_videoinfoheader2(s, st);
00664 avio_skip(pb, FFMAX(size - consumed, 0));
00665 } else {
00666 if (ff_guidcmp(formattype, ff_format_none))
00667 av_log(s, AV_LOG_WARNING, "unknown formattype:"FF_PRI_GUID"\n", FF_ARG_GUID(formattype));
00668 avio_skip(pb, size);
00669 }
00670
00671 if (!memcmp(subtype + 4, (const uint8_t[]){FF_MEDIASUBTYPE_BASE_GUID}, 12)) {
00672 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, AV_RL32(subtype));
00673 } else {
00674 st->codec->codec_id = ff_codec_guid_get_id(ff_video_guids, subtype);
00675 }
00676 if (st->codec->codec_id == AV_CODEC_ID_NONE)
00677 av_log(s, AV_LOG_WARNING, "unknown subtype:"FF_PRI_GUID"\n", FF_ARG_GUID(subtype));
00678 return st;
00679 } else if (!ff_guidcmp(mediatype, mediatype_mpeg2_pes) &&
00680 !ff_guidcmp(subtype, mediasubtype_dvb_subtitle)) {
00681 st = new_stream(s, st, sid, AVMEDIA_TYPE_SUBTITLE);
00682 if (!st)
00683 return NULL;
00684 if (ff_guidcmp(formattype, ff_format_none))
00685 av_log(s, AV_LOG_WARNING, "unknown formattype:"FF_PRI_GUID"\n", FF_ARG_GUID(formattype));
00686 avio_skip(pb, size);
00687 st->codec->codec_id = AV_CODEC_ID_DVB_SUBTITLE;
00688 return st;
00689 } else if (!ff_guidcmp(mediatype, mediatype_mstvcaption) &&
00690 (!ff_guidcmp(subtype, mediasubtype_teletext) || !ff_guidcmp(subtype, mediasubtype_dtvccdata))) {
00691 st = new_stream(s, st, sid, AVMEDIA_TYPE_SUBTITLE);
00692 if (!st)
00693 return NULL;
00694 if (ff_guidcmp(formattype, ff_format_none))
00695 av_log(s, AV_LOG_WARNING, "unknown formattype:"FF_PRI_GUID"\n", FF_ARG_GUID(formattype));
00696 avio_skip(pb, size);
00697 st->codec->codec_id = !ff_guidcmp(subtype, mediasubtype_teletext) ? AV_CODEC_ID_DVB_TELETEXT : AV_CODEC_ID_EIA_608;
00698 return st;
00699 } else if (!ff_guidcmp(mediatype, mediatype_mpeg2_sections) &&
00700 !ff_guidcmp(subtype, mediasubtype_mpeg2_sections)) {
00701 if (ff_guidcmp(formattype, ff_format_none))
00702 av_log(s, AV_LOG_WARNING, "unknown formattype:"FF_PRI_GUID"\n", FF_ARG_GUID(formattype));
00703 avio_skip(pb, size);
00704 return NULL;
00705 }
00706
00707 av_log(s, AV_LOG_WARNING, "unknown media type, mediatype:"FF_PRI_GUID
00708 ", subtype:"FF_PRI_GUID", formattype:"FF_PRI_GUID"\n",
00709 FF_ARG_GUID(mediatype), FF_ARG_GUID(subtype), FF_ARG_GUID(formattype));
00710 avio_skip(pb, size);
00711 return NULL;
00712 }
00713
00714 enum {
00715 SEEK_TO_DATA = 0,
00716 SEEK_TO_PTS,
00717 };
00718
00726 static int parse_chunks(AVFormatContext *s, int mode, int64_t seekts, int *len_ptr)
00727 {
00728 WtvContext *wtv = s->priv_data;
00729 AVIOContext *pb = wtv->pb;
00730 while (!url_feof(pb)) {
00731 ff_asf_guid g;
00732 int len, sid, consumed;
00733
00734 ff_get_guid(pb, &g);
00735 len = avio_rl32(pb);
00736 if (len < 32)
00737 break;
00738 sid = avio_rl32(pb) & 0x7FFF;
00739 avio_skip(pb, 8);
00740 consumed = 32;
00741
00742 if (!ff_guidcmp(g, ff_stream_guid)) {
00743 if (ff_find_stream_index(s, sid) < 0) {
00744 ff_asf_guid mediatype, subtype, formattype;
00745 int size;
00746 avio_skip(pb, 28);
00747 ff_get_guid(pb, &mediatype);
00748 ff_get_guid(pb, &subtype);
00749 avio_skip(pb, 12);
00750 ff_get_guid(pb, &formattype);
00751 size = avio_rl32(pb);
00752 parse_media_type(s, 0, sid, mediatype, subtype, formattype, size);
00753 consumed += 92 + size;
00754 }
00755 } else if (!ff_guidcmp(g, ff_stream2_guid)) {
00756 int stream_index = ff_find_stream_index(s, sid);
00757 if (stream_index >= 0 && !((WtvStream*)s->streams[stream_index]->priv_data)->seen_data) {
00758 ff_asf_guid mediatype, subtype, formattype;
00759 int size;
00760 avio_skip(pb, 12);
00761 ff_get_guid(pb, &mediatype);
00762 ff_get_guid(pb, &subtype);
00763 avio_skip(pb, 12);
00764 ff_get_guid(pb, &formattype);
00765 size = avio_rl32(pb);
00766 parse_media_type(s, s->streams[stream_index], sid, mediatype, subtype, formattype, size);
00767 consumed += 76 + size;
00768 }
00769 } else if (!ff_guidcmp(g, EVENTID_AudioDescriptorSpanningEvent) ||
00770 !ff_guidcmp(g, EVENTID_CtxADescriptorSpanningEvent) ||
00771 !ff_guidcmp(g, EVENTID_CSDescriptorSpanningEvent) ||
00772 !ff_guidcmp(g, EVENTID_StreamIDSpanningEvent) ||
00773 !ff_guidcmp(g, EVENTID_SubtitleSpanningEvent) ||
00774 !ff_guidcmp(g, EVENTID_TeletextSpanningEvent)) {
00775 int stream_index = ff_find_stream_index(s, sid);
00776 if (stream_index >= 0) {
00777 AVStream *st = s->streams[stream_index];
00778 uint8_t buf[258];
00779 const uint8_t *pbuf = buf;
00780 int buf_size;
00781
00782 avio_skip(pb, 8);
00783 consumed += 8;
00784 if (!ff_guidcmp(g, EVENTID_CtxADescriptorSpanningEvent) ||
00785 !ff_guidcmp(g, EVENTID_CSDescriptorSpanningEvent)) {
00786 avio_skip(pb, 6);
00787 consumed += 6;
00788 }
00789
00790 buf_size = FFMIN(len - consumed, sizeof(buf));
00791 avio_read(pb, buf, buf_size);
00792 consumed += buf_size;
00793 ff_parse_mpeg2_descriptor(s, st, 0, &pbuf, buf + buf_size, NULL, 0, 0, NULL);
00794 }
00795 } else if (!ff_guidcmp(g, EVENTID_AudioTypeSpanningEvent)) {
00796 int stream_index = ff_find_stream_index(s, sid);
00797 if (stream_index >= 0) {
00798 AVStream *st = s->streams[stream_index];
00799 int audio_type;
00800 avio_skip(pb, 8);
00801 audio_type = avio_r8(pb);
00802 if (audio_type == 2)
00803 st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
00804 else if (audio_type == 3)
00805 st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED;
00806 consumed += 9;
00807 }
00808 } else if (!ff_guidcmp(g, EVENTID_DVBScramblingControlSpanningEvent)) {
00809 int stream_index = ff_find_stream_index(s, sid);
00810 if (stream_index >= 0) {
00811 avio_skip(pb, 12);
00812 if (avio_rl32(pb))
00813 av_log(s, AV_LOG_WARNING, "DVB scrambled stream detected (st:%d), decoding will likely fail\n", stream_index);
00814 consumed += 16;
00815 }
00816 } else if (!ff_guidcmp(g, EVENTID_LanguageSpanningEvent)) {
00817 int stream_index = ff_find_stream_index(s, sid);
00818 if (stream_index >= 0) {
00819 AVStream *st = s->streams[stream_index];
00820 uint8_t language[4];
00821 avio_skip(pb, 12);
00822 avio_read(pb, language, 3);
00823 if (language[0]) {
00824 language[3] = 0;
00825 av_dict_set(&st->metadata, "language", language, 0);
00826 if (!strcmp(language, "nar") || !strcmp(language, "NAR"))
00827 st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED;
00828 }
00829 consumed += 15;
00830 }
00831 } else if (!ff_guidcmp(g, ff_timestamp_guid)) {
00832 int stream_index = ff_find_stream_index(s, sid);
00833 if (stream_index >= 0) {
00834 avio_skip(pb, 8);
00835 wtv->pts = avio_rl64(pb);
00836 consumed += 16;
00837 if (wtv->pts == -1)
00838 wtv->pts = AV_NOPTS_VALUE;
00839 else {
00840 wtv->last_valid_pts = wtv->pts;
00841 if (wtv->epoch == AV_NOPTS_VALUE || wtv->pts < wtv->epoch)
00842 wtv->epoch = wtv->pts;
00843 if (mode == SEEK_TO_PTS && wtv->pts >= seekts) {
00844 avio_skip(pb, WTV_PAD8(len) - consumed);
00845 return 0;
00846 }
00847 }
00848 }
00849 } else if (!ff_guidcmp(g, ff_data_guid)) {
00850 int stream_index = ff_find_stream_index(s, sid);
00851 if (mode == SEEK_TO_DATA && stream_index >= 0 && len > 32 && s->streams[stream_index]->priv_data) {
00852 WtvStream *wst = s->streams[stream_index]->priv_data;
00853 wst->seen_data = 1;
00854 if (len_ptr) {
00855 *len_ptr = len;
00856 }
00857 return stream_index;
00858 }
00859 } else if (
00860 !ff_guidcmp(g, (const ff_asf_guid){0x14,0x56,0x1A,0x0C,0xCD,0x30,0x40,0x4F,0xBC,0xBF,0xD0,0x3E,0x52,0x30,0x62,0x07}) ||
00861 !ff_guidcmp(g, (const ff_asf_guid){0x02,0xAE,0x5B,0x2F,0x8F,0x7B,0x60,0x4F,0x82,0xD6,0xE4,0xEA,0x2F,0x1F,0x4C,0x99}) ||
00862 !ff_guidcmp(g, ff_DSATTRIB_TRANSPORT_PROPERTIES) ||
00863 !ff_guidcmp(g, (const ff_asf_guid){0xCC,0x32,0x64,0xDD,0x29,0xE2,0xDB,0x40,0x80,0xF6,0xD2,0x63,0x28,0xD2,0x76,0x1F}) ||
00864 !ff_guidcmp(g, (const ff_asf_guid){0xE5,0xC5,0x67,0x90,0x5C,0x4C,0x05,0x42,0x86,0xC8,0x7A,0xFE,0x20,0xFE,0x1E,0xFA}) ||
00865 !ff_guidcmp(g, (const ff_asf_guid){0x80,0x6D,0xF3,0x41,0x32,0x41,0xC2,0x4C,0xB1,0x21,0x01,0xA4,0x32,0x19,0xD8,0x1B}) ||
00866 !ff_guidcmp(g, (const ff_asf_guid){0x51,0x1D,0xAB,0x72,0xD2,0x87,0x9B,0x48,0xBA,0x11,0x0E,0x08,0xDC,0x21,0x02,0x43}) ||
00867 !ff_guidcmp(g, (const ff_asf_guid){0x65,0x8F,0xFC,0x47,0xBB,0xE2,0x34,0x46,0x9C,0xEF,0xFD,0xBF,0xE6,0x26,0x1D,0x5C}) ||
00868 !ff_guidcmp(g, (const ff_asf_guid){0xCB,0xC5,0x68,0x80,0x04,0x3C,0x2B,0x49,0xB4,0x7D,0x03,0x08,0x82,0x0D,0xCE,0x51}) ||
00869 !ff_guidcmp(g, (const ff_asf_guid){0xBC,0x2E,0xAF,0x82,0xA6,0x30,0x64,0x42,0xA8,0x0B,0xAD,0x2E,0x13,0x72,0xAC,0x60}) ||
00870 !ff_guidcmp(g, (const ff_asf_guid){0x1E,0xBE,0xC3,0xC5,0x43,0x92,0xDC,0x11,0x85,0xE5,0x00,0x12,0x3F,0x6F,0x73,0xB9}) ||
00871 !ff_guidcmp(g, (const ff_asf_guid){0x3B,0x86,0xA2,0xB1,0xEB,0x1E,0xC3,0x44,0x8C,0x88,0x1C,0xA3,0xFF,0xE3,0xE7,0x6A}) ||
00872 !ff_guidcmp(g, (const ff_asf_guid){0x4E,0x7F,0x4C,0x5B,0xC4,0xD0,0x38,0x4B,0xA8,0x3E,0x21,0x7F,0x7B,0xBF,0x52,0xE7}) ||
00873 !ff_guidcmp(g, (const ff_asf_guid){0x63,0x36,0xEB,0xFE,0xA1,0x7E,0xD9,0x11,0x83,0x08,0x00,0x07,0xE9,0x5E,0xAD,0x8D}) ||
00874 !ff_guidcmp(g, (const ff_asf_guid){0x70,0xE9,0xF1,0xF8,0x89,0xA4,0x4C,0x4D,0x83,0x73,0xB8,0x12,0xE0,0xD5,0xF8,0x1E}) ||
00875 !ff_guidcmp(g, (const ff_asf_guid){0x96,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D}) ||
00876 !ff_guidcmp(g, (const ff_asf_guid){0x97,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D}) ||
00877 !ff_guidcmp(g, (const ff_asf_guid){0xA1,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D}) ||
00878 !ff_guidcmp(g, (const ff_asf_guid){0xF7,0x10,0x02,0xB9,0xEE,0x7C,0xED,0x4E,0xBD,0x7F,0x05,0x40,0x35,0x86,0x18,0xA1})) {
00879
00880 } else
00881 av_log(s, AV_LOG_WARNING, "unsupported chunk:"FF_PRI_GUID"\n", FF_ARG_GUID(g));
00882
00883 avio_skip(pb, WTV_PAD8(len) - consumed);
00884 }
00885 return AVERROR_EOF;
00886 }
00887
00888 static int read_header(AVFormatContext *s)
00889 {
00890 WtvContext *wtv = s->priv_data;
00891 int root_sector, root_size;
00892 uint8_t root[WTV_SECTOR_SIZE];
00893 AVIOContext *pb;
00894 int64_t timeline_pos;
00895 int ret;
00896
00897 wtv->epoch =
00898 wtv->pts =
00899 wtv->last_valid_pts = AV_NOPTS_VALUE;
00900
00901
00902 avio_skip(s->pb, 0x30);
00903 root_size = avio_rl32(s->pb);
00904 if (root_size > sizeof(root)) {
00905 av_log(s, AV_LOG_ERROR, "root directory size exceeds sector size\n");
00906 return AVERROR_INVALIDDATA;
00907 }
00908 avio_skip(s->pb, 4);
00909 root_sector = avio_rl32(s->pb);
00910
00911 avio_seek(s->pb, root_sector << WTV_SECTOR_BITS, SEEK_SET);
00912 root_size = avio_read(s->pb, root, root_size);
00913 if (root_size < 0)
00914 return AVERROR_INVALIDDATA;
00915
00916
00917 wtv->pb = wtvfile_open(s, root, root_size, ff_timeline_le16);
00918 if (!wtv->pb) {
00919 av_log(s, AV_LOG_ERROR, "timeline data missing\n");
00920 return AVERROR_INVALIDDATA;
00921 }
00922
00923 ret = parse_chunks(s, SEEK_TO_DATA, 0, 0);
00924 if (ret < 0)
00925 return ret;
00926 avio_seek(wtv->pb, -32, SEEK_CUR);
00927
00928 timeline_pos = avio_tell(s->pb);
00929
00930
00931 pb = wtvfile_open(s, root, root_size, ff_table_0_entries_legacy_attrib_le16);
00932 if (pb) {
00933 parse_legacy_attrib(s, pb);
00934 wtvfile_close(pb);
00935 }
00936
00937
00938 if (s->nb_streams) {
00939 AVStream *st = s->streams[0];
00940 pb = wtvfile_open(s, root, root_size, ff_table_0_entries_time_le16);
00941 if (pb) {
00942 while(1) {
00943 uint64_t timestamp = avio_rl64(pb);
00944 uint64_t frame_nb = avio_rl64(pb);
00945 if (url_feof(pb))
00946 break;
00947 ff_add_index_entry(&wtv->index_entries, &wtv->nb_index_entries, &wtv->index_entries_allocated_size,
00948 0, timestamp, frame_nb, 0, AVINDEX_KEYFRAME);
00949 }
00950 wtvfile_close(pb);
00951
00952 if (wtv->nb_index_entries) {
00953 pb = wtvfile_open(s, root, root_size, ff_timeline_table_0_entries_Events_le16);
00954 if (pb) {
00955 int i;
00956 while (1) {
00957 uint64_t frame_nb = avio_rl64(pb);
00958 uint64_t position = avio_rl64(pb);
00959 if (url_feof(pb))
00960 break;
00961 for (i = wtv->nb_index_entries - 1; i >= 0; i--) {
00962 AVIndexEntry *e = wtv->index_entries + i;
00963 if (frame_nb > e->size)
00964 break;
00965 if (position > e->pos)
00966 e->pos = position;
00967 }
00968 }
00969 wtvfile_close(pb);
00970 st->duration = wtv->index_entries[wtv->nb_index_entries - 1].timestamp;
00971 }
00972 }
00973 }
00974 }
00975
00976 avio_seek(s->pb, timeline_pos, SEEK_SET);
00977 return 0;
00978 }
00979
00980 static int read_packet(AVFormatContext *s, AVPacket *pkt)
00981 {
00982 WtvContext *wtv = s->priv_data;
00983 AVIOContext *pb = wtv->pb;
00984 int stream_index, len, ret;
00985
00986 stream_index = parse_chunks(s, SEEK_TO_DATA, 0, &len);
00987 if (stream_index < 0)
00988 return stream_index;
00989
00990 ret = av_get_packet(pb, pkt, len - 32);
00991 if (ret < 0)
00992 return ret;
00993 pkt->stream_index = stream_index;
00994 pkt->pts = wtv->pts;
00995 avio_skip(pb, WTV_PAD8(len) - len);
00996 return 0;
00997 }
00998
00999 static int read_seek(AVFormatContext *s, int stream_index,
01000 int64_t ts, int flags)
01001 {
01002 WtvContext *wtv = s->priv_data;
01003 AVIOContext *pb = wtv->pb;
01004 AVStream *st = s->streams[0];
01005 int64_t ts_relative;
01006 int i;
01007
01008 if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
01009 return AVERROR(ENOSYS);
01010
01011
01012
01013 ts_relative = ts;
01014 if (wtv->epoch != AV_NOPTS_VALUE)
01015 ts_relative -= wtv->epoch;
01016
01017 i = ff_index_search_timestamp(wtv->index_entries, wtv->nb_index_entries, ts_relative, flags);
01018 if (i < 0) {
01019 if (wtv->last_valid_pts == AV_NOPTS_VALUE || ts < wtv->last_valid_pts) {
01020 if (avio_seek(pb, 0, SEEK_SET) < 0)
01021 return -1;
01022 } else if (st->duration != AV_NOPTS_VALUE && ts_relative > st->duration && wtv->nb_index_entries) {
01023 if (avio_seek(pb, wtv->index_entries[wtv->nb_index_entries - 1].pos, SEEK_SET) < 0)
01024 return -1;
01025 }
01026 if (parse_chunks(s, SEEK_TO_PTS, ts, 0) < 0)
01027 return AVERROR(ERANGE);
01028 return 0;
01029 }
01030 if (avio_seek(pb, wtv->index_entries[i].pos, SEEK_SET) < 0)
01031 return -1;
01032 wtv->pts = wtv->index_entries[i].timestamp;
01033 if (wtv->epoch != AV_NOPTS_VALUE)
01034 wtv->pts += wtv->epoch;
01035 wtv->last_valid_pts = wtv->pts;
01036 return 0;
01037 }
01038
01039 static int read_close(AVFormatContext *s)
01040 {
01041 WtvContext *wtv = s->priv_data;
01042 av_freep(&wtv->index_entries);
01043 wtvfile_close(wtv->pb);
01044 return 0;
01045 }
01046
01047 AVInputFormat ff_wtv_demuxer = {
01048 .name = "wtv",
01049 .long_name = NULL_IF_CONFIG_SMALL("Windows Television (WTV)"),
01050 .priv_data_size = sizeof(WtvContext),
01051 .read_probe = read_probe,
01052 .read_header = read_header,
01053 .read_packet = read_packet,
01054 .read_seek = read_seek,
01055 .read_close = read_close,
01056 .flags = AVFMT_SHOW_IDS,
01057 };