FFmpeg
Loading...
Searching...
No Matches
wtvdec.c
Go to the documentation of this file.
1/*
2 * Windows Television (WTV) demuxer
3 * Copyright (c) 2010-2011 Peter Ross <pross@xvid.org>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * Windows Television (WTV) demuxer
25 * @author Peter Ross <pross@xvid.org>
26 */
27
28#include <inttypes.h>
29
32#include "libavutil/intfloat.h"
33#include "libavutil/mem.h"
34#include "libavutil/qsort.h"
35#include "avformat.h"
36#include "avio_internal.h"
37#include "demux.h"
38#include "internal.h"
39#include "wtv.h"
40#include "mpegts.h"
41
42/* Macros for formatting GUIDs */
43#define PRI_PRETTY_GUID \
44 "%08"PRIx32"-%04"PRIx16"-%04"PRIx16"-%02x%02x%02x%02x%02x%02x%02x%02x"
45#define ARG_PRETTY_GUID(g) \
46 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]
47#define LEN_PRETTY_GUID 35
48
49/*
50 * File system routines
51 */
52
53typedef struct WtvFile {
54 AVIOContext *pb_filesystem; /**< file system (AVFormatContext->pb) */
55
56 int sector_bits; /**< sector shift bits; used to convert sector number into pb_filesystem offset */
57 uint32_t *sectors; /**< file allocation table */
58 int nb_sectors; /**< number of sectors */
59
60 int error;
63} WtvFile;
64
66{
67 return avio_seek(pb, (sector << WTV_SECTOR_BITS) + offset, SEEK_SET);
68}
69
70/**
71 * @return bytes read, AVERROR_EOF on end of file, or <0 on error
72 */
73static int wtvfile_read_packet(void *opaque, uint8_t *buf, int buf_size)
74{
75 WtvFile *wf = opaque;
76 AVIOContext *pb = wf->pb_filesystem;
77 int nread = 0, n = 0;
78
79 if (wf->error || pb->error)
80 return -1;
81 if (wf->position >= wf->length || avio_feof(pb))
82 return AVERROR_EOF;
83
84 buf_size = FFMIN(buf_size, wf->length - wf->position);
85 while(nread < buf_size) {
86 int remaining_in_sector = (1 << wf->sector_bits) - (wf->position & ((1 << wf->sector_bits) - 1));
87 int read_request = FFMIN(buf_size - nread, remaining_in_sector);
88
89 n = avio_read(pb, buf, read_request);
90 if (n <= 0)
91 break;
92 nread += n;
93 buf += n;
94 wf->position += n;
95 if (n == remaining_in_sector) {
96 int i = wf->position >> wf->sector_bits;
97 if (i >= wf->nb_sectors ||
98 (wf->sectors[i] != wf->sectors[i - 1] + (1 << (wf->sector_bits - WTV_SECTOR_BITS)) &&
99 seek_by_sector(pb, wf->sectors[i], 0) < 0)) {
100 wf->error = 1;
101 break;
102 }
103 }
104 }
105 return nread ? nread : n;
106}
107
108/**
109 * @return position (or file length)
110 */
111static int64_t wtvfile_seek(void *opaque, int64_t offset, int whence)
112{
113 WtvFile *wf = opaque;
114 AVIOContext *pb = wf->pb_filesystem;
115
116 if (whence == AVSEEK_SIZE)
117 return wf->length;
118 else if (whence == SEEK_CUR)
119 offset = wf->position + offset;
120 else if (whence == SEEK_END)
121 offset = wf->length;
122
125 offset & ((1 << wf->sector_bits) - 1)) < 0;
126 wf->position = offset;
127 return offset;
128}
129
130static int cmp_sector(const uint32_t *a, const uint32_t *b)
131{
132 return (*a > *b) - (*a < *b);
133}
134
135/**
136 * read non-zero integers (le32) from input stream
137 * @param pb
138 * @param[out] data destination
139 * @param count maximum number of integers to read
140 * @return total number of integers read
141 */
142static int read_ints(AVIOContext *pb, uint32_t *data, int count)
143{
144 int i, total = 0;
145 for (i = 0; i < count; i++) {
146 if ((data[total] = avio_rl32(pb)))
147 total++;
148 }
149 return total;
150}
151
152/**
153 * Open file
154 * @param first_sector First sector
155 * @param length Length of file (bytes)
156 * @param depth File allocation table depth
157 * @return NULL on error
158 */
159static AVIOContext * wtvfile_open_sector(unsigned first_sector, uint64_t length, int depth, AVFormatContext *s)
160{
161 AVIOContext *pb;
162 WtvFile *wf;
163 uint8_t *buffer;
165
166 if (seek_by_sector(s->pb, first_sector, 0) < 0)
167 return NULL;
168
169 wf = av_mallocz(sizeof(WtvFile));
170 if (!wf)
171 return NULL;
172
173 if (depth == 0) {
174 wf->sectors = av_malloc(sizeof(uint32_t));
175 if (!wf->sectors) {
176 av_free(wf);
177 return NULL;
178 }
179 wf->sectors[0] = first_sector;
180 wf->nb_sectors = 1;
181 } else if (depth == 1) {
183 if (!wf->sectors) {
184 av_free(wf);
185 return NULL;
186 }
187 wf->nb_sectors = read_ints(s->pb, wf->sectors, WTV_SECTOR_SIZE / 4);
188 } else if (depth == 2) {
189 uint32_t sectors1[WTV_SECTOR_SIZE / 4];
190 int nb_sectors1 = read_ints(s->pb, sectors1, WTV_SECTOR_SIZE / 4);
191 int i;
192
193 wf->sectors = av_calloc(nb_sectors1, 1 << WTV_SECTOR_BITS);
194 if (!wf->sectors) {
195 av_free(wf);
196 return NULL;
197 }
198 wf->nb_sectors = 0;
199 for (i = 0; i < nb_sectors1; i++) {
200 if (seek_by_sector(s->pb, sectors1[i], 0) < 0)
201 break;
202 wf->nb_sectors += read_ints(s->pb, wf->sectors + wf->nb_sectors, WTV_SECTOR_SIZE / 4);
203 }
204 } else {
205 av_log(s, AV_LOG_ERROR, "unsupported file allocation table depth (0x%x)\n", depth);
206 av_free(wf);
207 return NULL;
208 }
209 wf->sector_bits = length & (1ULL<<63) ? WTV_SECTOR_BITS : WTV_BIGSECTOR_BITS;
210
211 if (!wf->nb_sectors) {
212 av_freep(&wf->sectors);
213 av_freep(&wf);
214 return NULL;
215 }
216
217 uint32_t *sorted = av_memdup(wf->sectors, wf->nb_sectors * sizeof(*wf->sectors));
218 uint32_t *tmp = av_malloc_array(wf->nb_sectors, sizeof(*tmp));
219 int i = 0;
220 if (sorted && tmp) {
221 AV_MSORT(sorted, tmp, wf->nb_sectors, uint32_t, cmp_sector);
222 for (i = 1; i < wf->nb_sectors && sorted[i] != sorted[i - 1]; i++)
223 ;
224 if (i < wf->nb_sectors)
225 av_log(s, AV_LOG_ERROR, "file allocation table lists sector %"PRIu32" more than once\n", sorted[i]);
226 }
227 av_free(sorted);
228 av_free(tmp);
229 if (i < wf->nb_sectors) {
230 av_freep(&wf->sectors);
231 av_freep(&wf);
232 return NULL;
233 }
234
235 size = avio_size(s->pb);
236 if (size >= 0 && (int64_t)wf->sectors[wf->nb_sectors - 1] << WTV_SECTOR_BITS > size)
237 av_log(s, AV_LOG_WARNING, "truncated file\n");
238
239 /* check length */
240 length &= 0xFFFFFFFFFFFF;
241 if (length > ((int64_t)wf->nb_sectors << wf->sector_bits)) {
242 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);
243 length = (int64_t)wf->nb_sectors << wf->sector_bits;
244 }
245 wf->length = length;
246
247 /* seek to initial sector */
248 wf->position = 0;
249 if (seek_by_sector(s->pb, wf->sectors[0], 0) < 0) {
250 av_freep(&wf->sectors);
251 av_freep(&wf);
252 return NULL;
253 }
254
255 wf->pb_filesystem = s->pb;
256 buffer = av_malloc(1 << wf->sector_bits);
257 if (!buffer) {
258 av_freep(&wf->sectors);
259 av_freep(&wf);
260 return NULL;
261 }
262
263 pb = avio_alloc_context(buffer, 1 << wf->sector_bits, 0, wf,
265 if (!pb) {
267 av_freep(&wf->sectors);
268 av_freep(&wf);
269 }
270 return pb;
271}
272
273/**
274 * Open file using filename
275 * @param[in] buf directory buffer
276 * @param buf_size directory buffer size
277 * @param[in] filename
278 * @param filename_size size of filename
279 * @return NULL on error
280 */
281static AVIOContext * wtvfile_open2(AVFormatContext *s, const uint8_t *buf, int buf_size, const uint8_t *filename, int filename_size)
282{
283 const uint8_t *buf_end = buf + buf_size;
284
285 while(buf + 48 <= buf_end) {
286 int dir_length, name_size, first_sector, depth;
287 uint64_t file_length;
288 const uint8_t *name;
289 if (ff_guidcmp(buf, ff_dir_entry_guid)) {
290 av_log(s, AV_LOG_ERROR, "unknown guid "FF_PRI_GUID", expected dir_entry_guid; "
291 "remaining directory entries ignored\n", FF_ARG_GUID(buf));
292 break;
293 }
294 dir_length = AV_RL16(buf + 16);
295 file_length = AV_RL64(buf + 24);
296 name_size = 2 * AV_RL32(buf + 32);
297 if (name_size < 0) {
299 "bad filename length, remaining directory entries ignored\n");
300 break;
301 }
302 if (dir_length == 0) {
304 "bad dir length, remaining directory entries ignored\n");
305 break;
306 }
307 if (48 + (int64_t)name_size > buf_end - buf) {
308 av_log(s, AV_LOG_ERROR, "filename exceeds buffer size; remaining directory entries ignored\n");
309 break;
310 }
311 first_sector = AV_RL32(buf + 40 + name_size);
312 depth = AV_RL32(buf + 44 + name_size);
313
314 /* compare file name; test optional null terminator */
315 name = buf + 40;
316 if (name_size >= filename_size &&
317 !memcmp(name, filename, filename_size) &&
318 (name_size < filename_size + 2 || !AV_RN16(name + filename_size)))
319 return wtvfile_open_sector(first_sector, file_length, depth, s);
320
321 buf += dir_length;
322 }
323 return NULL;
324}
325
326#define wtvfile_open(s, buf, buf_size, filename) \
327 wtvfile_open2(s, buf, buf_size, filename, sizeof(filename))
328
329/**
330 * Close file opened with wtvfile_open_sector(), or wtv_open()
331 */
333{
334 WtvFile *wf = pb->opaque;
335 av_freep(&wf->sectors);
336 av_freep(&pb->opaque);
337 av_freep(&pb->buffer);
339}
340
341/*
342 * Main demuxer
343 */
344
345typedef struct WtvStream {
347} WtvStream;
348
349typedef struct WtvContext {
350 AVIOContext *pb; /**< timeline file */
352 int64_t pts; /**< pts for next data chunk */
353 int64_t last_valid_pts; /**< latest valid pts, used for interactive seeking */
354
355 /* maintain private seek index, as the AVIndexEntry->pos is relative to the
356 start of the 'timeline' file, not the file system (AVFormatContext->pb) */
360} WtvContext;
361
362/* WTV GUIDs */
364 {0x48,0xC0,0xCE,0x5D,0xB9,0xD0,0x63,0x41,0x87,0x2C,0x4F,0x32,0x22,0x3B,0xE8,0x8A};
366 {0x6D,0x66,0x92,0xE2,0x02,0x9C,0x8D,0x44,0xAA,0x8D,0x78,0x1A,0x93,0xFD,0xC3,0x95};
368 {0x1C,0xD4,0x7B,0x10,0xDA,0xA6,0x91,0x46,0x83,0x69,0x11,0xB2,0xCD,0xAA,0x28,0x8E};
370 {0xE6,0xA2,0xB4,0x3A,0x47,0x42,0x34,0x4B,0x89,0x6C,0x30,0xAF,0xA5,0xD2,0x1C,0x24};
372 {0xD9,0x79,0xE7,0xEf,0xF0,0x97,0x86,0x47,0x80,0x0D,0x95,0xCF,0x50,0x5D,0xDC,0x66};
374 {0xC4,0xE1,0xD4,0x4B,0xA1,0x90,0x09,0x41,0x82,0x36,0x27,0xF0,0x0E,0x7D,0xCC,0x5B};
376 {0x68,0xAB,0xF1,0xCA,0x53,0xE1,0x41,0x4D,0xA6,0xB3,0xA7,0xC9,0x98,0xDB,0x75,0xEE};
378 {0x50,0xD9,0x99,0x95,0x33,0x5F,0x17,0x46,0xAF,0x7C,0x1E,0x54,0xB5,0x10,0xDA,0xA3};
380 {0xBE,0xBF,0x1C,0x50,0x49,0xB8,0xCE,0x42,0x9B,0xE9,0x3D,0xB8,0x69,0xFB,0x82,0xB3};
381
382/* Windows media GUIDs */
383
384/* Media types */
386 {0x81,0xEB,0x36,0xE4,0x4F,0x52,0xCE,0x11,0x9F,0x53,0x00,0x20,0xAF,0x0B,0xA7,0x70};
388 {0x6C,0x17,0x5F,0x45,0x06,0x4B,0xCE,0x47,0x9A,0xEF,0x8C,0xAE,0xF7,0x3D,0xF7,0xB5};
390 {0x20,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA};
392 {0x89,0x8A,0x8B,0xB8,0x49,0xB0,0x80,0x4C,0xAD,0xCF,0x58,0x98,0x98,0x5E,0x22,0xC1};
393
394/* Media subtypes */
396 {0xC3,0xCB,0xFF,0x34,0xB3,0xD5,0x71,0x41,0x90,0x02,0xD4,0xC6,0x03,0x01,0x69,0x7F};
398 {0xE3,0x76,0x2A,0xF7,0x0A,0xEB,0xD0,0x11,0xAC,0xE4,0x00,0x00,0xC0,0xCC,0x16,0xBA};
400 {0xAA,0xDD,0x2A,0xF5,0xF0,0x36,0xF5,0x43,0x95,0xEA,0x6D,0x86,0x64,0x84,0x26,0x2A};
402 {0x79,0x85,0x9F,0x4A,0xF8,0x6B,0x92,0x43,0x8A,0x6D,0xD2,0xDD,0x09,0xFA,0x78,0x61};
403
404static int read_probe(const AVProbeData *p)
405{
406 return ff_guidcmp(p->buf, ff_wtv_guid) ? 0 : AVPROBE_SCORE_MAX;
407}
408
409/**
410 * Convert crazy time (100ns since 1 Jan 0001) to av time (unix timestamp in microseconds)
411 */
413{
414 return (value / 10LL) - 719162LL*86400000000LL;
415}
416
417/**
418 * Convert OLE DATE to av time (unix timestamp in microseconds)
419 */
421{
422 return (av_int2double(value) - 25569.0) * 86400000000;
423}
424
425static void get_attachment(AVFormatContext *s, AVIOContext *pb, int length)
426{
427 char mime[1024];
428 char description[1024];
429 unsigned int filesize;
430 AVStream *st;
431 int64_t pos = avio_tell(pb);
432
433 avio_get_str16le(pb, INT_MAX, mime, sizeof(mime));
434 if (strcmp(mime, "image/jpeg"))
435 goto done;
436
437 avio_r8(pb);
438 avio_get_str16le(pb, INT_MAX, description, sizeof(description));
439 filesize = avio_rl32(pb);
440 if (!filesize)
441 goto done;
442
443 if (ff_add_attached_pic(s, NULL, pb, NULL, filesize) < 0)
444 goto done;
445 st = s->streams[s->nb_streams - 1];
446 av_dict_set(&st->metadata, "title", description, 0);
448 st->id = -1;
449done:
450 avio_seek(pb, pos + length, SEEK_SET);
451}
452
453static void get_tag(AVFormatContext *s, AVIOContext *pb, const char *key, int type, int length)
454{
455 char buf[LEN_PRETTY_GUID + 1], *bufp = buf;
456 unsigned dict_flags = 0;
457
458 if (!strcmp(key, "WM/MediaThumbType")) {
459 avio_skip(pb, length);
460 return;
461 }
462
463 if (type == 0 && length == 4) {
464 snprintf(buf, sizeof(buf), "%u", avio_rl32(pb));
465 } else if (type == 1) {
466 int buflen = FFMIN(length + length / 2U + 1, INT_MAX);
467 bufp = av_malloc(buflen);
468 if (!bufp)
469 return;
470 avio_get_str16le(pb, length, bufp, buflen);
471 if (!*bufp) {
472 av_free(bufp);
473 return;
474 }
475 dict_flags = AV_DICT_DONT_STRDUP_VAL;
476 } else if (type == 3 && length == 4) {
477 strcpy(buf, avio_rl32(pb) ? "true" : "false");
478 } else if (type == 4 && length == 8) {
479 int64_t num = avio_rl64(pb);
480 if (!strcmp(key, "WM/EncodingTime") ||
481 !strcmp(key, "WM/MediaOriginalBroadcastDateTime")) {
483 return;
484 } else if (!strcmp(key, "WM/WMRVEncodeTime") ||
485 !strcmp(key, "WM/WMRVEndTime")) {
487 return;
488 } else if (!strcmp(key, "WM/WMRVExpirationDate")) {
490 return;
491 } else if (!strcmp(key, "WM/WMRVBitrate"))
492 snprintf(buf, sizeof(buf), "%f", av_int2double(num));
493 else
494 snprintf(buf, sizeof(buf), "%"PRIi64, num);
495 } else if (type == 5 && length == 2) {
496 snprintf(buf, sizeof(buf), "%u", avio_rl16(pb));
497 } else if (type == 6 && length == 16) {
498 ff_asf_guid guid;
499 avio_read(pb, guid, 16);
500 snprintf(buf, sizeof(buf), PRI_PRETTY_GUID, ARG_PRETTY_GUID(guid));
501 } else if (type == 2 && !strcmp(key, "WM/Picture")) {
502 get_attachment(s, pb, length);
503 return;
504 } else {
505 av_log(s, AV_LOG_WARNING, "unsupported metadata entry; key:%s, type:%d, length:0x%x\n", key, type, length);
506 avio_skip(pb, length);
507 return;
508 }
509
510 av_dict_set(&s->metadata, key, bufp, dict_flags);
511}
512
513/**
514 * Parse metadata entries
515 */
517{
518 ff_asf_guid guid;
519 int length, type;
520 while(!avio_feof(pb)) {
521 char key[1024];
522 ff_get_guid(pb, &guid);
523 type = avio_rl32(pb);
524 length = avio_rl32(pb);
525 if (length <= 0)
526 break;
527 if (ff_guidcmp(&guid, ff_metadata_guid)) {
528 av_log(s, AV_LOG_WARNING, "unknown guid "FF_PRI_GUID", expected metadata_guid; "
529 "remaining metadata entries ignored\n", FF_ARG_GUID(guid));
530 break;
531 }
532 avio_get_str16le(pb, INT_MAX, key, sizeof(key));
533 get_tag(s, pb, key, type, length);
534 }
535
537}
538
539/**
540 * parse VIDEOINFOHEADER2 structure
541 * @return bytes consumed
542 */
544{
545 WtvContext *wtv = s->priv_data;
546 AVIOContext *pb = wtv->pb;
547
548 avio_skip(pb, 72); // picture aspect ratio is unreliable
550
551 return 72 + 40;
552}
553
554/**
555 * Parse MPEG1WAVEFORMATEX extradata structure
556 */
558{
559 /* fwHeadLayer */
560 switch (AV_RL16(st->codecpar->extradata)) {
561 case 0x0001 : st->codecpar->codec_id = AV_CODEC_ID_MP1; break;
562 case 0x0002 : st->codecpar->codec_id = AV_CODEC_ID_MP2; break;
563 case 0x0004 : st->codecpar->codec_id = AV_CODEC_ID_MP3; break;
564 }
565
566 st->codecpar->bit_rate = AV_RL32(st->codecpar->extradata + 2); /* dwHeadBitrate */
567
568 /* dwHeadMode */
569 switch (AV_RL16(st->codecpar->extradata + 6)) {
570 case 1 :
571 case 2 :
573 break;
575 break;
576 }
577}
578
579/**
580 * Initialise stream
581 * @param st Stream to initialise, or NULL to create and initialise new stream
582 * @return NULL on error
583 */
585{
586 if (st) {
587 if (st->codecpar->extradata) {
589 st->codecpar->extradata_size = 0;
590 }
591 } else {
592 WtvStream *wst = av_mallocz(sizeof(WtvStream));
593 if (!wst)
594 return NULL;
596 if (!st) {
597 av_free(wst);
598 return NULL;
599 }
600 st->id = sid;
601 st->priv_data = wst;
602 }
605 avpriv_set_pts_info(st, 64, 1, 10000000);
606 return st;
607}
608
609/**
610 * parse Media Type structure and populate stream
611 * @param st Stream, or NULL to create new stream
612 * @param mediatype Mediatype GUID
613 * @param subtype Subtype GUID
614 * @param formattype Format GUID
615 * @param size Size of format buffer
616 * @return NULL on error
617 */
619 ff_asf_guid mediatype, ff_asf_guid subtype,
620 ff_asf_guid formattype, uint64_t size)
621{
622 WtvContext *wtv = s->priv_data;
623 AVIOContext *pb = wtv->pb;
626 ff_asf_guid actual_subtype;
627 ff_asf_guid actual_formattype;
628
629 if (size < 32) {
630 av_log(s, AV_LOG_WARNING, "format buffer size underflow\n");
631 avio_skip(pb, size);
632 return NULL;
633 }
634
635 avio_skip(pb, size - 32);
636 ff_get_guid(pb, &actual_subtype);
637 ff_get_guid(pb, &actual_formattype);
638 if (avio_feof(pb))
639 return NULL;
640 avio_seek(pb, -size, SEEK_CUR);
641
642 st = parse_media_type(s, st, sid, mediatype, actual_subtype, actual_formattype, size - 32);
643 avio_skip(pb, 32);
644 return st;
645 } else if (!ff_guidcmp(mediatype, ff_mediatype_audio)) {
646 st = new_stream(s, st, sid, AVMEDIA_TYPE_AUDIO);
647 if (!st)
648 return NULL;
649 if (!ff_guidcmp(formattype, ff_format_waveformatex)) {
650 int ret = ff_get_wav_header(s, pb, st->codecpar, size, 0);
651 if (ret < 0)
652 return NULL;
653 } else {
654 if (ff_guidcmp(formattype, ff_format_none))
655 av_log(s, AV_LOG_WARNING, "unknown formattype:"FF_PRI_GUID"\n", FF_ARG_GUID(formattype));
656 avio_skip(pb, size);
657 }
658
659 if (!memcmp(subtype + 4, (const uint8_t[]){FF_MEDIASUBTYPE_BASE_GUID}, 12)) {
661 } else if (!ff_guidcmp(subtype, mediasubtype_mpeg1payload)) {
662 if (st->codecpar->extradata && st->codecpar->extradata_size >= 22)
664 else
665 av_log(s, AV_LOG_WARNING, "MPEG1WAVEFORMATEX underflow\n");
666 } else {
669 av_log(s, AV_LOG_WARNING, "unknown subtype:"FF_PRI_GUID"\n", FF_ARG_GUID(subtype));
670 }
671 return st;
672 } else if (!ff_guidcmp(mediatype, ff_mediatype_video)) {
673 st = new_stream(s, st, sid, AVMEDIA_TYPE_VIDEO);
674 if (!st)
675 return NULL;
676 if (!ff_guidcmp(formattype, ff_format_videoinfo2)) {
677 int consumed = parse_videoinfoheader2(s, st);
678 avio_skip(pb, FFMAX(size - consumed, 0));
679 } else if (!ff_guidcmp(formattype, ff_format_mpeg2_video)) {
680 uint64_t consumed = parse_videoinfoheader2(s, st);
681 /* ignore extradata; files produced by windows media center contain meaningless mpeg1 sequence header */
682 avio_skip(pb, FFMAX(size - consumed, 0));
683 } else {
684 if (ff_guidcmp(formattype, ff_format_none))
685 av_log(s, AV_LOG_WARNING, "unknown formattype:"FF_PRI_GUID"\n", FF_ARG_GUID(formattype));
686 avio_skip(pb, size);
687 }
688
689 if (!memcmp(subtype + 4, (const uint8_t[]){FF_MEDIASUBTYPE_BASE_GUID}, 12)) {
691 } else {
693 }
695 av_log(s, AV_LOG_WARNING, "unknown subtype:"FF_PRI_GUID"\n", FF_ARG_GUID(subtype));
696 return st;
697 } else if (!ff_guidcmp(mediatype, mediatype_mpeg2_pes) &&
699 st = new_stream(s, st, sid, AVMEDIA_TYPE_SUBTITLE);
700 if (!st)
701 return NULL;
702 if (ff_guidcmp(formattype, ff_format_none))
703 av_log(s, AV_LOG_WARNING, "unknown formattype:"FF_PRI_GUID"\n", FF_ARG_GUID(formattype));
704 avio_skip(pb, size);
706 return st;
707 } else if (!ff_guidcmp(mediatype, mediatype_mstvcaption) &&
709 st = new_stream(s, st, sid, AVMEDIA_TYPE_SUBTITLE);
710 if (!st)
711 return NULL;
712 if (ff_guidcmp(formattype, ff_format_none))
713 av_log(s, AV_LOG_WARNING, "unknown formattype:"FF_PRI_GUID"\n", FF_ARG_GUID(formattype));
714 avio_skip(pb, size);
716 return st;
717 } else if (!ff_guidcmp(mediatype, mediatype_mpeg2_sections) &&
719 if (ff_guidcmp(formattype, ff_format_none))
720 av_log(s, AV_LOG_WARNING, "unknown formattype:"FF_PRI_GUID"\n", FF_ARG_GUID(formattype));
721 avio_skip(pb, size);
722 return NULL;
723 }
724
725 av_log(s, AV_LOG_WARNING, "unknown media type, mediatype:"FF_PRI_GUID
726 ", subtype:"FF_PRI_GUID", formattype:"FF_PRI_GUID"\n",
727 FF_ARG_GUID(mediatype), FF_ARG_GUID(subtype), FF_ARG_GUID(formattype));
728 avio_skip(pb, size);
729 return NULL;
730}
731
732enum {
735};
736
737/**
738 * Try to seek over a broken chunk
739 * @return <0 on error
740 */
741static int recover(WtvContext *wtv, uint64_t broken_pos)
742{
743 AVIOContext *pb = wtv->pb;
744 int i;
745 for (i = 0; i < wtv->nb_index_entries; i++) {
746 if (wtv->index_entries[i].pos > broken_pos) {
747 int64_t ret = avio_seek(pb, wtv->index_entries[i].pos, SEEK_SET);
748 if (ret < 0)
749 return ret;
750 wtv->pts = wtv->index_entries[i].timestamp;
751 return 0;
752 }
753 }
754 return AVERROR_INVALIDDATA;
755}
756
757/**
758 * Parse WTV chunks
759 * @param mode SEEK_TO_DATA or SEEK_TO_PTS
760 * @param seekts timestamp
761 * @param[out] len_ptr Length of data chunk
762 * @return stream index of data chunk, or <0 on error
763 */
764static int parse_chunks(AVFormatContext *s, int mode, int64_t seekts, int *len_ptr)
765{
766 WtvContext *wtv = s->priv_data;
767 AVIOContext *pb = wtv->pb;
768 int ret;
769 while (!avio_feof(pb)) {
771 int len, sid, consumed;
772
773 ff_get_guid(pb, &g);
774 len = avio_rl32(pb);
775 if (len < 32 || len > INT_MAX - 7) {
776 int ret;
777 if (avio_feof(pb))
778 return AVERROR_EOF;
779 av_log(s, AV_LOG_WARNING, "encountered broken chunk\n");
780 if ((ret = recover(wtv, avio_tell(pb) - 20)) < 0)
781 return ret;
782 continue;
783 }
784 sid = avio_rl32(pb) & 0x7FFF;
785 avio_skip(pb, 8);
786 consumed = 32;
787
789 if (ff_find_stream_index(s, sid) < 0) {
790 ff_asf_guid mediatype, subtype, formattype;
791 int size;
792 avio_skip(pb, 28);
793 ff_get_guid(pb, &mediatype);
794 ff_get_guid(pb, &subtype);
795 avio_skip(pb, 12);
796 ff_get_guid(pb, &formattype);
797 size = avio_rl32(pb);
798 if (size < 0 || size > INT_MAX - 92 - consumed)
799 return AVERROR_INVALIDDATA;
800 parse_media_type(s, 0, sid, mediatype, subtype, formattype, size);
801 consumed += 92 + size;
802 }
803 } else if (!ff_guidcmp(g, ff_stream2_guid)) {
804 int stream_index = ff_find_stream_index(s, sid);
805 if (stream_index >= 0 && s->streams[stream_index]->priv_data && !((WtvStream*)s->streams[stream_index]->priv_data)->seen_data) {
806 ff_asf_guid mediatype, subtype, formattype;
807 int size;
808 avio_skip(pb, 12);
809 ff_get_guid(pb, &mediatype);
810 ff_get_guid(pb, &subtype);
811 avio_skip(pb, 12);
812 ff_get_guid(pb, &formattype);
813 size = avio_rl32(pb);
814 if (size < 0 || size > INT_MAX - 76 - consumed)
815 return AVERROR_INVALIDDATA;
816 parse_media_type(s, s->streams[stream_index], sid, mediatype, subtype, formattype, size);
817 consumed += 76 + size;
818 }
825 int stream_index = ff_find_stream_index(s, sid);
826 if (stream_index >= 0) {
827 AVStream *st = s->streams[stream_index];
828 uint8_t buf[258] = {0};
829 const uint8_t *pbuf = buf;
830 int buf_size;
831
832 avio_skip(pb, 8);
833 consumed += 8;
836 avio_skip(pb, 6);
837 consumed += 6;
838 }
839
840 buf_size = FFMIN(len - consumed, sizeof(buf));
841 ret = ffio_read_size(pb, buf, buf_size);
842 if (ret < 0)
843 return ret;
844 consumed += buf_size;
845 ff_parse_mpeg2_descriptor(s, st, 0, -1, &pbuf, buf + buf_size, NULL, 0, 0, NULL);
846 }
848 int stream_index = ff_find_stream_index(s, sid);
849 if (stream_index >= 0) {
850 AVStream *st = s->streams[stream_index];
851 int audio_type;
852 avio_skip(pb, 8);
853 audio_type = avio_r8(pb);
854 if (audio_type == 2)
856 else if (audio_type == 3)
858 consumed += 9;
859 }
861 int stream_index = ff_find_stream_index(s, sid);
862 if (stream_index >= 0) {
863 avio_skip(pb, 12);
864 if (avio_rl32(pb))
865 av_log(s, AV_LOG_WARNING, "DVB scrambled stream detected (st:%d), decoding will likely fail\n", stream_index);
866 consumed += 16;
867 }
869 int stream_index = ff_find_stream_index(s, sid);
870 if (stream_index >= 0) {
871 AVStream *st = s->streams[stream_index];
872 uint8_t language[4];
873 avio_skip(pb, 12);
874 if (avio_read(pb, language, 3) != 3)
875 return AVERROR_INVALIDDATA;
876 if (language[0]) {
877 language[3] = 0;
878 av_dict_set(&st->metadata, "language", language, 0);
879 if (!strcmp(language, "nar") || !strcmp(language, "NAR"))
881 }
882 consumed += 15;
883 }
884 } else if (!ff_guidcmp(g, ff_timestamp_guid)) {
885 int stream_index = ff_find_stream_index(s, sid);
886 if (stream_index >= 0) {
887 avio_skip(pb, 8);
888 wtv->pts = avio_rl64(pb);
889 consumed += 16;
890 if (wtv->pts == -1)
891 wtv->pts = AV_NOPTS_VALUE;
892 else {
893 wtv->last_valid_pts = wtv->pts;
894 if (wtv->epoch == AV_NOPTS_VALUE || wtv->pts < wtv->epoch)
895 wtv->epoch = wtv->pts;
896 if (mode == SEEK_TO_PTS && wtv->pts >= seekts) {
897 avio_skip(pb, WTV_PAD8(len) - consumed);
898 return 0;
899 }
900 }
901 }
902 } else if (!ff_guidcmp(g, ff_data_guid)) {
903 int stream_index = ff_find_stream_index(s, sid);
904 if (mode == SEEK_TO_DATA && stream_index >= 0 && len > 32 && s->streams[stream_index]->priv_data) {
905 WtvStream *wst = s->streams[stream_index]->priv_data;
906 wst->seen_data = 1;
907 if (len_ptr) {
908 *len_ptr = len;
909 }
910 return stream_index;
911 }
912 } else if (!ff_guidcmp(g, /* DSATTRIB_WMDRMProtectionInfo */ (const ff_asf_guid){0x83,0x95,0x74,0x40,0x9D,0x6B,0xEC,0x4E,0xB4,0x3C,0x67,0xA1,0x80,0x1E,0x1A,0x9B})) {
913 int stream_index = ff_find_stream_index(s, sid);
914 if (stream_index >= 0)
915 av_log(s, AV_LOG_WARNING, "encrypted stream detected (st:%d), decoding will likely fail\n", stream_index);
916 } else if (
917 !ff_guidcmp(g, /* DSATTRIB_CAPTURE_STREAMTIME */ (const ff_asf_guid){0x14,0x56,0x1A,0x0C,0xCD,0x30,0x40,0x4F,0xBC,0xBF,0xD0,0x3E,0x52,0x30,0x62,0x07}) ||
918 !ff_guidcmp(g, /* DSATTRIB_PBDATAG_ATTRIBUTE */ (const ff_asf_guid){0x79,0x66,0xB5,0xE0,0xB9,0x12,0xCC,0x43,0xB7,0xDF,0x57,0x8C,0xAA,0x5A,0x7B,0x63}) ||
919 !ff_guidcmp(g, /* DSATTRIB_PicSampleSeq */ (const ff_asf_guid){0x02,0xAE,0x5B,0x2F,0x8F,0x7B,0x60,0x4F,0x82,0xD6,0xE4,0xEA,0x2F,0x1F,0x4C,0x99}) ||
920 !ff_guidcmp(g, /* DSATTRIB_TRANSPORT_PROPERTIES */ ff_DSATTRIB_TRANSPORT_PROPERTIES) ||
921 !ff_guidcmp(g, /* dvr_ms_vid_frame_rep_data */ (const ff_asf_guid){0xCC,0x32,0x64,0xDD,0x29,0xE2,0xDB,0x40,0x80,0xF6,0xD2,0x63,0x28,0xD2,0x76,0x1F}) ||
922 !ff_guidcmp(g, /* EVENTID_ChannelChangeSpanningEvent */ (const ff_asf_guid){0xE5,0xC5,0x67,0x90,0x5C,0x4C,0x05,0x42,0x86,0xC8,0x7A,0xFE,0x20,0xFE,0x1E,0xFA}) ||
923 !ff_guidcmp(g, /* EVENTID_ChannelInfoSpanningEvent */ (const ff_asf_guid){0x80,0x6D,0xF3,0x41,0x32,0x41,0xC2,0x4C,0xB1,0x21,0x01,0xA4,0x32,0x19,0xD8,0x1B}) ||
924 !ff_guidcmp(g, /* EVENTID_ChannelTypeSpanningEvent */ (const ff_asf_guid){0x51,0x1D,0xAB,0x72,0xD2,0x87,0x9B,0x48,0xBA,0x11,0x0E,0x08,0xDC,0x21,0x02,0x43}) ||
925 !ff_guidcmp(g, /* EVENTID_PIDListSpanningEvent */ (const ff_asf_guid){0x65,0x8F,0xFC,0x47,0xBB,0xE2,0x34,0x46,0x9C,0xEF,0xFD,0xBF,0xE6,0x26,0x1D,0x5C}) ||
926 !ff_guidcmp(g, /* EVENTID_SignalAndServiceStatusSpanningEvent */ (const ff_asf_guid){0xCB,0xC5,0x68,0x80,0x04,0x3C,0x2B,0x49,0xB4,0x7D,0x03,0x08,0x82,0x0D,0xCE,0x51}) ||
927 !ff_guidcmp(g, /* EVENTID_StreamTypeSpanningEvent */ (const ff_asf_guid){0xBC,0x2E,0xAF,0x82,0xA6,0x30,0x64,0x42,0xA8,0x0B,0xAD,0x2E,0x13,0x72,0xAC,0x60}) ||
928 !ff_guidcmp(g, (const ff_asf_guid){0x1E,0xBE,0xC3,0xC5,0x43,0x92,0xDC,0x11,0x85,0xE5,0x00,0x12,0x3F,0x6F,0x73,0xB9}) ||
929 !ff_guidcmp(g, (const ff_asf_guid){0x3B,0x86,0xA2,0xB1,0xEB,0x1E,0xC3,0x44,0x8C,0x88,0x1C,0xA3,0xFF,0xE3,0xE7,0x6A}) ||
930 !ff_guidcmp(g, (const ff_asf_guid){0x4E,0x7F,0x4C,0x5B,0xC4,0xD0,0x38,0x4B,0xA8,0x3E,0x21,0x7F,0x7B,0xBF,0x52,0xE7}) ||
931 !ff_guidcmp(g, (const ff_asf_guid){0x63,0x36,0xEB,0xFE,0xA1,0x7E,0xD9,0x11,0x83,0x08,0x00,0x07,0xE9,0x5E,0xAD,0x8D}) ||
932 !ff_guidcmp(g, (const ff_asf_guid){0x70,0xE9,0xF1,0xF8,0x89,0xA4,0x4C,0x4D,0x83,0x73,0xB8,0x12,0xE0,0xD5,0xF8,0x1E}) ||
936 !ff_guidcmp(g, (const ff_asf_guid){0xF7,0x10,0x02,0xB9,0xEE,0x7C,0xED,0x4E,0xBD,0x7F,0x05,0x40,0x35,0x86,0x18,0xA1})) {
937 //ignore known guids
938 } else
939 av_log(s, AV_LOG_WARNING, "unsupported chunk:"FF_PRI_GUID"\n", FF_ARG_GUID(g));
940
941 if (avio_feof(pb))
942 break;
943
944 avio_skip(pb, WTV_PAD8(len) - consumed);
945 }
946 return AVERROR_EOF;
947}
948
950{
951 WtvContext *wtv = s->priv_data;
952 unsigned root_sector;
953 int root_size;
954 uint8_t root[WTV_SECTOR_SIZE];
955 AVIOContext *pb;
956 int64_t timeline_pos;
957 int64_t ret;
958
959 wtv->epoch =
960 wtv->pts =
962
963 /* read root directory sector */
964 avio_skip(s->pb, 0x30);
965 root_size = avio_rl32(s->pb);
966 if (root_size > sizeof(root)) {
967 av_log(s, AV_LOG_ERROR, "root directory size exceeds sector size\n");
968 return AVERROR_INVALIDDATA;
969 }
970 avio_skip(s->pb, 4);
971 root_sector = avio_rl32(s->pb);
972
973 ret = seek_by_sector(s->pb, root_sector, 0);
974 if (ret < 0)
975 return ret;
976 root_size = avio_read(s->pb, root, root_size);
977 if (root_size < 0)
978 return AVERROR_INVALIDDATA;
979
980 /* parse chunks up until first data chunk */
981 wtv->pb = wtvfile_open(s, root, root_size, ff_timeline_le16);
982 if (!wtv->pb) {
983 av_log(s, AV_LOG_ERROR, "timeline data missing\n");
984 return AVERROR_INVALIDDATA;
985 }
986
987 ret = parse_chunks(s, SEEK_TO_DATA, 0, 0);
988 if (ret < 0) {
989 wtvfile_close(wtv->pb);
990 return ret;
991 }
992 avio_seek(wtv->pb, -32, SEEK_CUR);
993
994 timeline_pos = avio_tell(s->pb); // save before opening another file
995
996 /* read metadata */
998 if (pb) {
1000 wtvfile_close(pb);
1001 }
1002
1003 s->ctx_flags |= AVFMTCTX_NOHEADER; // Needed for noStreams.wtv
1004
1005 /* read seek index */
1006 if (s->nb_streams) {
1007 AVStream *st = s->streams[0];
1009 if (pb) {
1010 while(1) {
1011 uint64_t timestamp = avio_rl64(pb);
1012 uint64_t frame_nb = avio_rl64(pb);
1013 if (avio_feof(pb))
1014 break;
1016 0, timestamp, frame_nb, 0, AVINDEX_KEYFRAME);
1017 }
1018 wtvfile_close(pb);
1019
1020 if (wtv->nb_index_entries) {
1022 if (pb) {
1023 AVIndexEntry *e = wtv->index_entries;
1024 AVIndexEntry *e_end = wtv->index_entries + wtv->nb_index_entries - 1;
1025 uint64_t last_position = 0;
1026 while (1) {
1027 uint64_t frame_nb = avio_rl64(pb);
1028 uint64_t position = avio_rl64(pb);
1029 while (e <= e_end && frame_nb > e->size) {
1030 e->pos = last_position;
1031 e++;
1032 }
1033 if (avio_feof(pb))
1034 break;
1035 last_position = position;
1036 }
1037 e_end->pos = last_position;
1038 wtvfile_close(pb);
1039 st->duration = e_end->timestamp;
1040 }
1041 }
1042 }
1043 }
1044
1045 avio_seek(s->pb, timeline_pos, SEEK_SET);
1046 return 0;
1047}
1048
1050{
1051 WtvContext *wtv = s->priv_data;
1052 AVIOContext *pb = wtv->pb;
1053 int stream_index, len, ret;
1054
1055 stream_index = parse_chunks(s, SEEK_TO_DATA, 0, &len);
1056 if (stream_index < 0)
1057 return stream_index;
1058
1059 ret = av_get_packet(pb, pkt, len - 32);
1060 if (ret < 0)
1061 return ret;
1062 pkt->stream_index = stream_index;
1063 pkt->pts = wtv->pts;
1064 avio_skip(pb, WTV_PAD8(len) - len);
1065 return 0;
1066}
1067
1068static int read_seek(AVFormatContext *s, int stream_index,
1069 int64_t ts, int flags)
1070{
1071 WtvContext *wtv = s->priv_data;
1072 AVIOContext *pb = wtv->pb;
1073 AVStream *st = s->streams[0];
1074 int64_t ts_relative;
1075 int i;
1076
1078 return AVERROR(ENOSYS);
1079
1080 /* timestamp adjustment is required because wtv->pts values are absolute,
1081 * whereas AVIndexEntry->timestamp values are relative to epoch. */
1082 ts_relative = ts;
1083 if (wtv->epoch != AV_NOPTS_VALUE)
1084 ts_relative -= wtv->epoch;
1085
1087 if (i < 0) {
1088 if (wtv->last_valid_pts == AV_NOPTS_VALUE || ts < wtv->last_valid_pts) {
1089 if (avio_seek(pb, 0, SEEK_SET) < 0)
1090 return -1;
1091 } else if (st->duration != AV_NOPTS_VALUE && ts_relative > st->duration && wtv->nb_index_entries) {
1092 if (avio_seek(pb, wtv->index_entries[wtv->nb_index_entries - 1].pos, SEEK_SET) < 0)
1093 return -1;
1094 }
1095 if (parse_chunks(s, SEEK_TO_PTS, ts, 0) < 0)
1096 return AVERROR(ERANGE);
1097 return 0;
1098 }
1099 if (avio_seek(pb, wtv->index_entries[i].pos, SEEK_SET) < 0)
1100 return -1;
1101 wtv->pts = wtv->index_entries[i].timestamp;
1102 if (wtv->epoch != AV_NOPTS_VALUE)
1103 wtv->pts += wtv->epoch;
1104 wtv->last_valid_pts = wtv->pts;
1105 return 0;
1106}
1107
1109{
1110 WtvContext *wtv = s->priv_data;
1111 av_freep(&wtv->index_entries);
1112 wtvfile_close(wtv->pb);
1113 return 0;
1114}
1115
1117 .p.name = "wtv",
1118 .p.long_name = NULL_IF_CONFIG_SMALL("Windows Television (WTV)"),
1119 .p.flags = AVFMT_SHOW_IDS,
1120 .priv_data_size = sizeof(WtvContext),
1126};
const FFInputFormat ff_wtv_demuxer
Definition wtvdec.c:1116
const AVMetadataConv ff_asf_metadata_conv[]
Definition asf.c:28
static int64_t ff_asf_filetime_to_avtime(int64_t filetime)
Definition asf.h:123
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:856
Main libavformat public API header.
#define AVINDEX_KEYFRAME
Definition avformat.h:630
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:485
#define AV_DISPOSITION_HEARING_IMPAIRED
The stream is intended for hearing impaired audiences.
Definition avformat.h:678
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition avformat.h:1351
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition avformat.h:2712
#define AVFMT_SHOW_IDS
Show format stream IDs numbers.
Definition avformat.h:498
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AV_DISPOSITION_VISUAL_IMPAIRED
The stream is intended for visually impaired audiences.
Definition avformat.h:682
#define AVSEEK_FLAG_FRAME
seeking based on frame number
Definition avformat.h:2714
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition avformat.h:613
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a UTF-16 string from pb and convert it to UTF-8.
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
Definition avio.h:468
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl16(AVIOContext *s)
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, const uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
Definition aviobuf.c:109
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
unsigned int avio_rl32(AVIOContext *s)
void avio_context_free(AVIOContext **s)
Free the supplied IO context and everything associated with it.
Definition aviobuf.c:126
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
uint64_t avio_rl64(AVIOContext *s)
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int ff_add_index_entry(AVIndexEntry **index_entries, int *nb_index_entries, unsigned int *index_entries_allocated_size, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Internal version of av_add_index_entry.
Definition seek.c:64
int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries, int64_t wanted_timestamp, int flags)
Internal version of av_index_search_timestamp.
Definition seek.c:132
int ff_find_stream_index(const AVFormatContext *s, int id)
Find stream index based on format-specific stream ID.
int ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb, AVBufferRef **buf, int size)
Add an attached pic to an AVStream.
static AVPacket * pkt
double value
Definition eval.c:102
static int64_t filesize(AVIOContext *pb)
Definition ffmpeg_mux.c:52
const char * key
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_DVB_SUBTITLE
Definition codec_id.h:571
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_MP1
Definition codec_id.h:499
@ AV_CODEC_ID_EIA_608
Definition codec_id.h:580
@ AV_CODEC_ID_MP2
Definition codec_id.h:457
@ AV_CODEC_ID_DVB_TELETEXT
Definition codec_id.h:577
@ AV_CODEC_ID_MJPEG
Definition codec_id.h:57
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:458
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_MONO
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition dict.h:79
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:87
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:408
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
int a
#define b
Definition input.c:43
static av_always_inline double av_int2double(uint64_t i)
Reinterpret a 64-bit integer as a double.
Definition intfloat.h:60
#define AV_RL64(p)
#define AV_RL32(p)
#define AV_RL16(p)
#define AV_RN16(p)
uint32_t type
Definition jpegmpfenc.c:80
unsigned offset
Definition libaomenc.c:763
int ff_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
Set a dictionary value to an ISO-8601 compliant timestamp string.
Definition utils.c:626
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition utils.c:143
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition metadata.c:26
int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type, int prg_id, const uint8_t **pp, const uint8_t *desc_list_end, Mp4Descr *mp4_descr, int mp4_descr_count, int pid, MpegTSContext *ts)
Parse an MPEG-2 descriptor.
Definition mpegts.c:2077
char * root
Definition mscl.c:248
const char data[16]
Definition mxf.c:149
#define av_malloc(s)
Definition ops_static.c:52
#define AV_MSORT(p, tmp, num, type, cmp)
Merge sort, this sort requires a temporary buffer and is stable, its worst case time is O(n log n)
Definition qsort.h:103
const char * name
Definition qsvenc.c:142
const AVCodecGuid ff_codec_wav_guids[]
Definition riff.c:661
const AVCodecTag ff_codec_bmp_tags[]
Definition riff.c:36
static av_always_inline int ff_guidcmp(const void *g1, const void *g2)
Definition riff.h:122
int ff_get_guid(AVIOContext *s, ff_asf_guid *g)
Definition riffdec.c:33
int ff_get_bmp_header(AVIOContext *pb, AVStream *st, uint32_t *size)
Read BITMAPINFOHEADER structure and set AVStream codec width, height and bits_per_encoded_sample fiel...
Definition riffdec.c:297
#define FF_PRI_GUID
Definition riff.h:105
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size, int big_endian)
Definition riffdec.c:141
enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps)
Definition riffdec.c:280
#define FF_ARG_GUID(g)
Definition riff.h:109
#define FF_MEDIASUBTYPE_BASE_GUID
Definition riff.h:115
uint8_t ff_asf_guid[16]
Definition riff.h:96
enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid)
Definition riffdec.c:45
enum AVMediaType codec_type
Definition rtp.c:37
unsigned int pos
Definition spdifenc.c:431
An AVChannelLayout holds information about the channel layout of audio data.
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1400
Bytestream IO Context.
Definition avio.h:160
unsigned char * buffer
Start of the buffer.
Definition avio.h:225
void * opaque
A private pointer, passed to the read/write/seek/... functions.
Definition avio.h:232
int error
contains the error code or 0 if no error happened
Definition avio.h:239
int64_t pos
Definition avformat.h:623
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition avformat.h:624
This structure stores compressed data.
Definition packet.h:587
This structure contains the data a format has to probe a file.
Definition avformat.h:473
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:827
AVDictionary * metadata
Definition avformat.h:848
void * priv_data
Definition avformat.h:793
int id
Format-specific stream ID.
Definition avformat.h:780
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition avformat.h:837
enum AVStreamParseType need_parsing
Definition internal.h:321
unsigned int index_entries_allocated_size
Definition wtvdec.c:359
int nb_index_entries
Definition wtvdec.c:358
int64_t pts
pts for next data chunk
Definition wtvdec.c:352
int64_t last_valid_pts
latest valid pts, used for interactive seeking
Definition wtvdec.c:353
AVIOContext * pb
timeline file
Definition wtvdec.c:350
int64_t epoch
Definition wtvdec.c:351
AVIndexEntry * index_entries
Definition wtvdec.c:357
uint32_t * sectors
file allocation table
Definition wtvdec.c:57
int64_t position
Definition wtvdec.c:61
AVIOContext * pb_filesystem
file system (AVFormatContext->pb)
Definition wtvdec.c:54
int error
Definition wtvdec.c:60
int nb_sectors
number of sectors
Definition wtvdec.c:58
int64_t length
Definition wtvdec.c:62
int sector_bits
sector shift bits; used to convert sector number into pb_filesystem offset
Definition wtvdec.c:56
int seen_data
Definition wtvdec.c:346
Definition swscale.c:71
#define av_free(p)
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int num
Definition error.c:23
static char buffer[20]
Definition seek.c:32
int size
const char * g
Definition vf_curves.c:128
int len
const ff_asf_guid ff_mediatype_audio
Definition wtv_common.c:41
const uint8_t ff_timeline_table_0_entries_Events_le16[62]
Definition wtv_common.c:52
const ff_asf_guid ff_format_mpeg2_video
Definition wtv_common.c:76
const ff_asf_guid ff_metadata_guid
Definition wtv_common.c:62
#define WTV_SECTOR_BITS
Definition wtv.h:28
const ff_asf_guid ff_format_none
Definition wtv_common.c:45
const ff_asf_guid ff_mediasubtype_cpfilters_processed
Definition wtv_common.c:68
const uint8_t ff_table_0_entries_time_le16[40]
Definition wtv_common.c:56
const ff_asf_guid ff_SBE2_STREAM_DESC_EVENT
Definition wtv_common.c:33
const ff_asf_guid ff_mediatype_video
Definition wtv_common.c:43
const ff_asf_guid ff_stream2_guid
Definition wtv_common.c:64
const AVCodecGuid ff_video_guids[]
Definition wtv_common.c:81
const ff_asf_guid ff_data_guid
Definition wtv_common.c:31
const ff_asf_guid ff_format_waveformatex
Definition wtv_common.c:74
const uint8_t ff_timeline_le16[16]
Definition wtv_common.c:50
const ff_asf_guid ff_format_videoinfo2
Definition wtv_common.c:78
const ff_asf_guid ff_timestamp_guid
Definition wtv_common.c:29
const ff_asf_guid ff_index_guid
Definition wtv_common.c:39
const ff_asf_guid ff_DSATTRIB_TRANSPORT_PROPERTIES
Definition wtv_common.c:60
#define WTV_BIGSECTOR_BITS
Definition wtv.h:30
const uint8_t ff_table_0_entries_legacy_attrib_le16[58]
Definition wtv_common.c:54
const ff_asf_guid ff_dir_entry_guid
Definition wtv_common.c:25
#define WTV_PAD8(x)
Definition wtv.h:31
const ff_asf_guid ff_sync_guid
Definition wtv_common.c:37
const ff_asf_guid ff_wtv_guid
Definition wtv_common.c:27
#define WTV_SECTOR_SIZE
Definition wtv.h:29
const ff_asf_guid ff_format_cpfilters_processed
Definition wtv_common.c:72
const ff_asf_guid ff_stream1_guid
Definition wtv_common.c:35
static AVStream * new_stream(AVFormatContext *s, AVStream *st, int sid, int codec_type)
Initialise stream.
Definition wtvdec.c:584
static int64_t crazytime_to_avtime(int64_t value)
Convert crazy time (100ns since 1 Jan 0001) to av time (unix timestamp in microseconds)
Definition wtvdec.c:412
#define PRI_PRETTY_GUID
Definition wtvdec.c:43
static const ff_asf_guid EVENTID_SubtitleSpanningEvent
Definition wtvdec.c:363
static int recover(WtvContext *wtv, uint64_t broken_pos)
Try to seek over a broken chunk.
Definition wtvdec.c:741
static const ff_asf_guid mediasubtype_mpeg2_sections
Definition wtvdec.c:401
#define wtvfile_open(s, buf, buf_size, filename)
Definition wtvdec.c:326
static AVIOContext * wtvfile_open2(AVFormatContext *s, const uint8_t *buf, int buf_size, const uint8_t *filename, int filename_size)
Open file using filename.
Definition wtvdec.c:281
static const ff_asf_guid mediatype_mstvcaption
Definition wtvdec.c:391
static int read_close(AVFormatContext *s)
Definition wtvdec.c:1108
static int parse_chunks(AVFormatContext *s, int mode, int64_t seekts, int *len_ptr)
Parse WTV chunks.
Definition wtvdec.c:764
static const ff_asf_guid mediatype_mpeg2_pes
Definition wtvdec.c:389
static int64_t seek_by_sector(AVIOContext *pb, int64_t sector, int64_t offset)
Definition wtvdec.c:65
static const ff_asf_guid EVENTID_CSDescriptorSpanningEvent
Definition wtvdec.c:371
static const ff_asf_guid EVENTID_AudioTypeSpanningEvent
Definition wtvdec.c:379
static int parse_videoinfoheader2(AVFormatContext *s, AVStream *st)
parse VIDEOINFOHEADER2 structure
Definition wtvdec.c:543
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition wtvdec.c:1049
static void get_tag(AVFormatContext *s, AVIOContext *pb, const char *key, int type, int length)
Definition wtvdec.c:453
static const ff_asf_guid mediatype_mpeg2_sections
Definition wtvdec.c:387
static void get_attachment(AVFormatContext *s, AVIOContext *pb, int length)
Definition wtvdec.c:425
static int read_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
Definition wtvdec.c:1068
static AVIOContext * wtvfile_open_sector(unsigned first_sector, uint64_t length, int depth, AVFormatContext *s)
Open file.
Definition wtvdec.c:159
static const ff_asf_guid mediasubtype_dvb_subtitle
Definition wtvdec.c:395
static const ff_asf_guid EVENTID_DVBScramblingControlSpanningEvent
Definition wtvdec.c:373
static int read_probe(const AVProbeData *p)
Definition wtvdec.c:404
#define ARG_PRETTY_GUID(g)
Definition wtvdec.c:45
static const ff_asf_guid mediasubtype_mpeg1payload
Definition wtvdec.c:385
static const ff_asf_guid EVENTID_CtxADescriptorSpanningEvent
Definition wtvdec.c:369
static void parse_legacy_attrib(AVFormatContext *s, AVIOContext *pb)
Parse metadata entries.
Definition wtvdec.c:516
#define LEN_PRETTY_GUID
Definition wtvdec.c:47
static int64_t wtvfile_seek(void *opaque, int64_t offset, int whence)
Definition wtvdec.c:111
static const ff_asf_guid mediasubtype_dtvccdata
Definition wtvdec.c:399
static const ff_asf_guid EVENTID_AudioDescriptorSpanningEvent
Definition wtvdec.c:367
@ SEEK_TO_DATA
Definition wtvdec.c:733
@ SEEK_TO_PTS
Definition wtvdec.c:734
static void parse_mpeg1waveformatex(AVStream *st)
Parse MPEG1WAVEFORMATEX extradata structure.
Definition wtvdec.c:557
static int wtvfile_read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition wtvdec.c:73
static const ff_asf_guid EVENTID_LanguageSpanningEvent
Definition wtvdec.c:365
static int read_header(AVFormatContext *s)
Definition wtvdec.c:949
static int64_t oledate_to_avtime(int64_t value)
Convert OLE DATE to av time (unix timestamp in microseconds)
Definition wtvdec.c:420
static const ff_asf_guid EVENTID_StreamIDSpanningEvent
Definition wtvdec.c:375
static int read_ints(AVIOContext *pb, uint32_t *data, int count)
read non-zero integers (le32) from input stream
Definition wtvdec.c:142
static const ff_asf_guid EVENTID_TeletextSpanningEvent
Definition wtvdec.c:377
static int cmp_sector(const uint32_t *a, const uint32_t *b)
Definition wtvdec.c:130
static AVStream * parse_media_type(AVFormatContext *s, AVStream *st, int sid, ff_asf_guid mediatype, ff_asf_guid subtype, ff_asf_guid formattype, uint64_t size)
parse Media Type structure and populate stream
Definition wtvdec.c:618
static void wtvfile_close(AVIOContext *pb)
Close file opened with wtvfile_open_sector(), or wtv_open()
Definition wtvdec.c:332
static const ff_asf_guid mediasubtype_teletext
Definition wtvdec.c:397