]> git.sesse.net Git - ffmpeg/blob - libavformat/wtv.c
reindent after last commit
[ffmpeg] / libavformat / wtv.c
1 /*
2  * Windows Television (WTV) demuxer
3  * Copyright (c) 2010 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 "libavutil/intreadwrite.h"
29 #include "avformat.h"
30 #include "riff.h"
31 #include "asf.h"
32 #include "mpegts.h"
33
34 /* Macros for formating GUIDs */
35 #define PRI_GUID \
36     "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
37 #define ARG_GUID(g) \
38     g[0],g[1],g[2],g[3],g[4],g[5],g[6],g[7],g[8],g[9],g[10],g[11],g[12],g[13],g[14],g[15]
39
40 typedef struct {
41     int seen_data;
42 } WtvStream;
43
44 typedef struct WtvContext {
45     uint64_t pts;
46 } WtvContext;
47
48 static int is_zero(uint8_t *v, int n)
49 {
50    int i;
51    for (i = 0; i < n; i++)
52       if (v[i]) return 0;
53    return 1;
54 }
55
56 typedef struct {
57     enum CodecID id;
58     ff_asf_guid guid;
59 } AVCodecGuid;
60
61 static enum CodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid)
62 {
63     int i;
64     for (i = 0; guids[i].id != CODEC_ID_NONE; i++) {
65         if (!ff_guidcmp(guids[i].guid, guid))
66             return guids[i].id;
67     }
68     return CODEC_ID_NONE;
69 }
70
71 /* WTV GUIDs */
72 static const ff_asf_guid wtv_guid =
73     {0xB7,0xD8,0x00,0x20,0x37,0x49,0xDA,0x11,0xA6,0x4E,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
74 static const ff_asf_guid meta_guid =
75     {0x5A,0xFE,0xD7,0x6D,0xC8,0x1D,0x8F,0x4A,0x99,0x22,0xFA,0xB1,0x1C,0x38,0x14,0x53};
76 static const ff_asf_guid timestamp_guid =
77     {0x5B,0x05,0xE6,0x1B,0x97,0xA9,0x49,0x43,0x88,0x17,0x1A,0x65,0x5A,0x29,0x8A,0x97};
78 static const ff_asf_guid data_guid =
79     {0x95,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
80 static const ff_asf_guid stream_guid =
81     {0xED,0xA4,0x13,0x23,0x2D,0xBF,0x4F,0x45,0xAD,0x8A,0xD9,0x5B,0xA7,0xF9,0x1F,0xEE};
82 static const ff_asf_guid stream2_guid =
83     {0xA2,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D};
84 static const ff_asf_guid EVENTID_SubtitleSpanningEvent =
85     {0x48,0xC0,0xCE,0x5D,0xB9,0xD0,0x63,0x41,0x87,0x2C,0x4F,0x32,0x22,0x3B,0xE8,0x8A};
86 static const ff_asf_guid EVENTID_LanguageSpanningEvent =
87     {0x6D,0x66,0x92,0xE2,0x02,0x9C,0x8D,0x44,0xAA,0x8D,0x78,0x1A,0x93,0xFD,0xC3,0x95};
88 static const ff_asf_guid EVENTID_AudioDescriptorSpanningEvent =
89     {0x1C,0xD4,0x7B,0x10,0xDA,0xA6,0x91,0x46,0x83,0x69,0x11,0xB2,0xCD,0xAA,0x28,0x8E};
90 static const ff_asf_guid EVENTID_CtxADescriptorSpanningEvent =
91     {0xE6,0xA2,0xB4,0x3A,0x47,0x42,0x34,0x4B,0x89,0x6C,0x30,0xAF,0xA5,0xD2,0x1C,0x24};
92 static const ff_asf_guid EVENTID_CSDescriptorSpanningEvent =
93     {0xD9,0x79,0xE7,0xEf,0xF0,0x97,0x86,0x47,0x80,0x0D,0x95,0xCF,0x50,0x5D,0xDC,0x66};
94 static const ff_asf_guid EVENTID_DVBScramblingControlSpanningEvent =
95     {0xC4,0xE1,0xD4,0x4B,0xA1,0x90,0x09,0x41,0x82,0x36,0x27,0xF0,0x0E,0x7D,0xCC,0x5B};
96 static const ff_asf_guid EVENTID_StreamIDSpanningEvent =
97     {0x68,0xAB,0xF1,0xCA,0x53,0xE1,0x41,0x4D,0xA6,0xB3,0xA7,0xC9,0x98,0xDB,0x75,0xEE};
98 static const ff_asf_guid EVENTID_TeletextSpanningEvent =
99     {0x50,0xD9,0x99,0x95,0x33,0x5F,0x17,0x46,0xAF,0x7C,0x1E,0x54,0xB5,0x10,0xDA,0xA3};
100
101 /* Windows media GUIDs */
102
103 #define MEDIASUBTYPE_BASE_GUID \
104     0x00,0x00,0x10,0x00,0x80,0x00,0x00,0xAA,0x00,0x38,0x9B,0x71
105
106 /* Media types */
107 static const ff_asf_guid mediatype_audio =
108     {'a','u','d','s',MEDIASUBTYPE_BASE_GUID};
109 static const ff_asf_guid mediatype_video =
110     {'v','i','d','s',MEDIASUBTYPE_BASE_GUID};
111 static const ff_asf_guid mediasubtype_mpeg1payload =
112     {0x81,0xEB,0x36,0xE4,0x4F,0x52,0xCE,0x11,0x9F,0x53,0x00,0x20,0xAF,0x0B,0xA7,0x70};
113 static const ff_asf_guid mediatype_mpeg2_sections =
114     {0x6C,0x17,0x5F,0x45,0x06,0x4B,0xCE,0x47,0x9A,0xEF,0x8C,0xAE,0xF7,0x3D,0xF7,0xB5};
115 static const ff_asf_guid mediatype_mpeg2_pes =
116     {0x20,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA};
117 static const ff_asf_guid mediatype_mstvcaption =
118     {0x89,0x8A,0x8B,0xB8,0x49,0xB0,0x80,0x4C,0xAD,0xCF,0x58,0x98,0x98,0x5E,0x22,0xC1};
119
120 /* Media subtypes */
121 static const ff_asf_guid mediasubtype_cpfilters_processed =
122     {0x28,0xBD,0xAD,0x46,0xD0,0x6F,0x96,0x47,0x93,0xB2,0x15,0x5C,0x51,0xDC,0x04,0x8D};
123 static const ff_asf_guid mediasubtype_dvb_subtitle =
124     {0xC3,0xCB,0xFF,0x34,0xB3,0xD5,0x71,0x41,0x90,0x02,0xD4,0xC6,0x03,0x01,0x69,0x7F};
125 static const ff_asf_guid mediasubtype_teletext =
126     {0xE3,0x76,0x2A,0xF7,0x0A,0xEB,0xD0,0x11,0xAC,0xE4,0x00,0x00,0xC0,0xCC,0x16,0xBA};
127 static const ff_asf_guid mediasubtype_dtvccdata =
128     {0xAA,0xDD,0x2A,0xF5,0xF0,0x36,0xF5,0x43,0x95,0xEA,0x6D,0x86,0x64,0x84,0x26,0x2A};
129 static const ff_asf_guid mediasubtype_mpeg2_sections =
130     {0x79,0x85,0x9F,0x4A,0xF8,0x6B,0x92,0x43,0x8A,0x6D,0xD2,0xDD,0x09,0xFA,0x78,0x61};
131
132 /* Formats */
133 static const ff_asf_guid format_cpfilters_processed =
134     {0x6F,0xB3,0x39,0x67,0x5F,0x1D,0xC2,0x4A,0x81,0x92,0x28,0xBB,0x0E,0x73,0xD1,0x6A};
135 static const ff_asf_guid format_waveformatex =
136     {0x81,0x9F,0x58,0x05,0x56,0xC3,0xCE,0x11,0xBF,0x01,0x00,0xAA,0x00,0x55,0x59,0x5A};
137 static const ff_asf_guid format_videoinfo2 =
138     {0xA0,0x76,0x2A,0xF7,0x0A,0xEB,0xD0,0x11,0xAC,0xE4,0x00,0x00,0xC0,0xCC,0x16,0xBA};
139 static const ff_asf_guid format_mpeg2_video =
140     {0xE3,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA};
141 static const ff_asf_guid format_none =
142     {0xD6,0x17,0x64,0x0F,0x18,0xC3,0xD0,0x11,0xA4,0x3F,0x00,0xA0,0xC9,0x22,0x31,0x96};
143
144 static const AVCodecGuid video_guids[] = {
145     {CODEC_ID_MPEG2VIDEO, {0x26,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA}},
146     {CODEC_ID_NONE}
147 };
148
149 static const AVCodecGuid audio_guids[] = {
150     {CODEC_ID_AC3,        {0x2C,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA}},
151     {CODEC_ID_EAC3,       {0xAF,0x87,0xFB,0xA7,0x02,0x2D,0xFB,0x42,0xA4,0xD4,0x05,0xCD,0x93,0x84,0x3B,0xDD}},
152     {CODEC_ID_MP2,        {0x2B,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA}},
153     {CODEC_ID_NONE}
154 };
155
156 static int read_probe(AVProbeData *p)
157 {
158     return ff_guidcmp(p->buf, wtv_guid) ? 0 : AVPROBE_SCORE_MAX;
159 }
160
161 /**
162  * parse VIDEOINFOHEADER2 structure
163  * @return bytes consumed
164  */
165 static int parse_videoinfoheader2(AVFormatContext *s, AVStream *st)
166 {
167     ByteIOContext *pb = s->pb;
168
169     url_fskip(pb, 72);  // picture aspect ratio is unreliable
170     ff_get_bmp_header(pb, st);
171
172     return 72 + 40;
173 }
174
175 /**
176  * Parse MPEG1WAVEFORMATEX extradata structure
177  */
178 static void parse_mpeg1waveformatex(AVStream *st)
179 {
180     /* fwHeadLayer */
181     switch (AV_RL16(st->codec->extradata)) {
182     case 0x0001 : st->codec->codec_id = CODEC_ID_MP1; break;
183     case 0x0002 : st->codec->codec_id = CODEC_ID_MP2; break;
184     case 0x0004 : st->codec->codec_id = CODEC_ID_MP3; break;
185     }
186
187     st->codec->bit_rate = AV_RL32(st->codec->extradata + 2); /* dwHeadBitrate */
188
189     /* dwHeadMode */
190     switch (AV_RL16(st->codec->extradata + 6)) {
191     case 1 : case 2 : case 4 : st->codec->channels = 2; break;
192     case 8 :                   st->codec->channels = 1; break;
193     }
194 }
195
196 /**
197  * Initialise stream
198  * @param st Stream to initialise, or NULL to create and initialise new stream
199  * @return NULL on error
200  */
201 static AVStream * new_stream(AVFormatContext *s, AVStream *st, int sid, int codec_type)
202 {
203     if (st) {
204         if (st->codec->extradata) {
205             av_freep(&st->codec->extradata);
206             st->codec->extradata_size = 0;
207         }
208     } else {
209         WtvStream *wst = av_mallocz(sizeof(WtvStream));
210         if (!wst)
211             return NULL;
212         st = av_new_stream(s, sid);
213         if (!st)
214             return NULL;
215         st->priv_data = wst;
216     }
217     st->codec->codec_type = codec_type;
218     st->need_parsing      = AVSTREAM_PARSE_FULL;
219     av_set_pts_info(st, 64, 1, 10000000);
220     return st;
221 }
222
223 /**
224  * parse Media Type structure and populate stream
225  * @param st         Stream, or NULL to create new stream
226  * @param mediatype  Mediatype GUID
227  * @param subtype    Subtype GUID
228  * @param formattype Format GUID
229  * @param size       Size of format buffer
230  * @return NULL on error
231  */
232 static AVStream * parse_media_type(AVFormatContext *s, AVStream *st, int sid,
233                                    ff_asf_guid mediatype, ff_asf_guid subtype,
234                                    ff_asf_guid formattype, int size)
235 {
236     ByteIOContext *pb = s->pb;
237     if (!ff_guidcmp(subtype, mediasubtype_cpfilters_processed) &&
238         !ff_guidcmp(formattype, format_cpfilters_processed)) {
239         ff_asf_guid actual_subtype;
240         ff_asf_guid actual_formattype;
241
242         if (size < 32) {
243             av_log(s, AV_LOG_WARNING, "format buffer size underflow\n");
244             url_fskip(pb, size);
245             return NULL;
246         }
247
248         url_fskip(pb, size - 32);
249         ff_get_guid(pb, &actual_subtype);
250         ff_get_guid(pb, &actual_formattype);
251         url_fseek(pb, -size, SEEK_CUR);
252
253         st = parse_media_type(s, st, sid, mediatype, actual_subtype, actual_formattype, size - 32);
254         url_fskip(pb, 32);
255         return st;
256     } else if (!ff_guidcmp(mediatype, mediatype_audio)) {
257         st = new_stream(s, st, sid, AVMEDIA_TYPE_AUDIO);
258         if (!st)
259             return NULL;
260         if (!ff_guidcmp(formattype, format_waveformatex)) {
261             ff_get_wav_header(pb, st->codec, size);
262         } else {
263             if (ff_guidcmp(formattype, format_none))
264                 av_log(s, AV_LOG_WARNING, "unknown formattype:"PRI_GUID"\n", ARG_GUID(formattype));
265             url_fskip(pb, size);
266         }
267
268         if (!memcmp(subtype + 4, (const uint8_t[]){MEDIASUBTYPE_BASE_GUID}, 12)) {
269             st->codec->codec_id = ff_wav_codec_get_id(AV_RL32(subtype), st->codec->bits_per_coded_sample);
270         } else if (!ff_guidcmp(subtype, mediasubtype_mpeg1payload)) {
271             if (st->codec->extradata && st->codec->extradata_size >= 22)
272                 parse_mpeg1waveformatex(st);
273             else
274                 av_log(s, AV_LOG_WARNING, "MPEG1WAVEFORMATEX underflow\n");
275         } else {
276             st->codec->codec_id = ff_codec_guid_get_id(audio_guids, subtype);
277             if (st->codec->codec_id == CODEC_ID_NONE)
278                 av_log(s, AV_LOG_WARNING, "unknown subtype:"PRI_GUID"\n", ARG_GUID(subtype));
279         }
280         return st;
281     } else if (!ff_guidcmp(mediatype, mediatype_video)) {
282         st = new_stream(s, st, sid, AVMEDIA_TYPE_VIDEO);
283         if (!st)
284             return NULL;
285         if (!ff_guidcmp(formattype, format_videoinfo2)) {
286             int consumed = parse_videoinfoheader2(s, st);
287             url_fskip(pb, FFMAX(size - consumed, 0));
288         } else if (!ff_guidcmp(formattype, format_mpeg2_video)) {
289             int consumed = parse_videoinfoheader2(s, st);
290             url_fskip(pb, FFMAX(size - consumed, 0));
291         } else {
292             if (ff_guidcmp(formattype, format_none))
293                 av_log(s, AV_LOG_WARNING, "unknown formattype:"PRI_GUID"\n", ARG_GUID(formattype));
294             url_fskip(pb, size);
295         }
296
297         if (!memcmp(subtype + 4, (const uint8_t[]){MEDIASUBTYPE_BASE_GUID}, 12)) {
298             st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, AV_RL32(subtype));
299         } else {
300             st->codec->codec_id = ff_codec_guid_get_id(video_guids, subtype);
301         }
302         if (st->codec->codec_id == CODEC_ID_NONE)
303             av_log(s, AV_LOG_WARNING, "unknown subtype:"PRI_GUID"\n", ARG_GUID(subtype));
304         return st;
305     } else if (!ff_guidcmp(mediatype, mediatype_mpeg2_pes) &&
306                !ff_guidcmp(subtype, mediasubtype_dvb_subtitle)) {
307         st = new_stream(s, st, sid, AVMEDIA_TYPE_SUBTITLE);
308         if (!st)
309             return NULL;
310         if (ff_guidcmp(formattype, format_none))
311             av_log(s, AV_LOG_WARNING, "unknown formattype:"PRI_GUID"\n", ARG_GUID(formattype));
312         url_fskip(pb, size);
313         st->codec->codec_id = CODEC_ID_DVB_SUBTITLE;
314         return st;
315     } else if (!ff_guidcmp(mediatype, mediatype_mstvcaption) &&
316                (!ff_guidcmp(subtype, mediasubtype_teletext) || !ff_guidcmp(subtype, mediasubtype_dtvccdata))) {
317         st = new_stream(s, st, sid, AVMEDIA_TYPE_SUBTITLE);
318         if (!st)
319             return NULL;
320         if (ff_guidcmp(formattype, format_none))
321             av_log(s, AV_LOG_WARNING, "unknown formattype:"PRI_GUID"\n", ARG_GUID(formattype));
322         url_fskip(pb, size);
323         st->codec->codec_id   = CODEC_ID_DVB_TELETEXT;
324         return st;
325     } else if (!ff_guidcmp(mediatype, mediatype_mpeg2_sections) &&
326                !ff_guidcmp(subtype, mediasubtype_mpeg2_sections)) {
327         if (ff_guidcmp(formattype, format_none))
328             av_log(s, AV_LOG_WARNING, "unknown formattype:"PRI_GUID"\n", ARG_GUID(formattype));
329         url_fskip(pb, size);
330         return NULL;
331     }
332
333     av_log(s, AV_LOG_WARNING, "unknown media type, mediatype:"PRI_GUID
334                               ", subtype:"PRI_GUID", formattype:"PRI_GUID"\n",
335                               ARG_GUID(mediatype), ARG_GUID(subtype), ARG_GUID(formattype));
336     url_fskip(pb, size);
337     return NULL;
338 }
339
340 enum {
341     SEEK_TO_DATA = 0,
342     SEEK_TO_BYTE,
343     SEEK_TO_PTS,
344 };
345
346 /**
347  * Parse WTV chunks
348  * @param mode SEEK_TO_DATA, SEEK_TO_BYTE, SEEK_TO_PTS
349  * @param seekts either byte position or timestamp
350  * @param[out] len Length of data chunk
351  * @return stream index of data chunk, or <0 on error
352  */
353 static int parse_chunks(AVFormatContext *s, int mode, int64_t seekts, int *len_ptr)
354 {
355     WtvContext *wtv = s->priv_data;
356     ByteIOContext *pb = s->pb;
357     while (!url_feof(pb)) {
358         ff_asf_guid g;
359         int len, sid, consumed;
360
361         if (mode == SEEK_TO_BYTE && url_ftell(pb) >= seekts)
362             return 0;
363
364         ff_get_guid(pb, &g);
365         if (is_zero(g, sizeof(ff_asf_guid)))
366             return AVERROR_EOF;
367
368         len = get_le32(pb);
369         if (len < 32)
370             break;
371         sid = get_le32(pb) & 0x7FFF;
372         url_fskip(pb, 8);
373         consumed = 32;
374
375         if (!ff_guidcmp(g, stream_guid)) {
376             if (ff_find_stream_index(s, sid) < 0) {
377                 ff_asf_guid mediatype, subtype, formattype;
378                 int size;
379                 consumed += 20;
380                 url_fskip(pb, 16);
381                 if (get_le32(pb)) {
382                     url_fskip(pb, 8);
383                     ff_get_guid(pb, &mediatype);
384                     ff_get_guid(pb, &subtype);
385                     url_fskip(pb, 12);
386                     ff_get_guid(pb, &formattype);
387                     size = get_le32(pb);
388                     parse_media_type(s, 0, sid, mediatype, subtype, formattype, size);
389                     consumed += 72 + size;
390                 }
391             }
392         } else if (!ff_guidcmp(g, stream2_guid)) {
393             int stream_index = ff_find_stream_index(s, sid);
394             if (stream_index >= 0 && !((WtvStream*)s->streams[stream_index]->priv_data)->seen_data) {
395                 ff_asf_guid mediatype, subtype, formattype;
396                 int size;
397                 url_fskip(pb, 12);
398                 ff_get_guid(pb, &mediatype);
399                 ff_get_guid(pb, &subtype);
400                 url_fskip(pb, 12);
401                 ff_get_guid(pb, &formattype);
402                 size = get_le32(pb);
403                 parse_media_type(s, s->streams[stream_index], sid, mediatype, subtype, formattype, size);
404                 consumed += 76 + size;
405             }
406         } else if (!ff_guidcmp(g, EVENTID_AudioDescriptorSpanningEvent) ||
407                    !ff_guidcmp(g, EVENTID_CtxADescriptorSpanningEvent) ||
408                    !ff_guidcmp(g, EVENTID_CSDescriptorSpanningEvent) ||
409                    !ff_guidcmp(g, EVENTID_StreamIDSpanningEvent) ||
410                    !ff_guidcmp(g, EVENTID_SubtitleSpanningEvent) ||
411                    !ff_guidcmp(g, EVENTID_TeletextSpanningEvent)) {
412             int stream_index = ff_find_stream_index(s, sid);
413             if (stream_index >= 0) {
414                 AVStream *st = s->streams[stream_index];
415                 uint8_t buf[258];
416                 const uint8_t *pbuf = buf;
417                 int buf_size;
418
419                 url_fskip(pb, 8);
420                 consumed += 8;
421                 if (!ff_guidcmp(g, EVENTID_CtxADescriptorSpanningEvent) ||
422                     !ff_guidcmp(g, EVENTID_CSDescriptorSpanningEvent)) {
423                     url_fskip(pb, 6);
424                     consumed += 6;
425                 }
426
427                 buf_size = FFMIN(len - consumed, sizeof(buf));
428                 get_buffer(pb, buf, buf_size);
429                 consumed += buf_size;
430                 ff_parse_mpeg2_descriptor(s, st, 0, &pbuf, buf + buf_size, 0, 0, 0, 0);
431             }
432         } else if (!ff_guidcmp(g, EVENTID_DVBScramblingControlSpanningEvent)) {
433             int stream_index = ff_find_stream_index(s, sid);
434             if (stream_index >= 0) {
435                 url_fskip(pb, 12);
436                 if (get_le32(pb))
437                     av_log(s, AV_LOG_WARNING, "DVB scrambled stream detected (st:%d), decoding will likely fail\n", stream_index);
438                 consumed += 16;
439             }
440         } else if (!ff_guidcmp(g, EVENTID_LanguageSpanningEvent)) {
441             int stream_index = ff_find_stream_index(s, sid);
442             if (stream_index >= 0) {
443                 AVStream *st = s->streams[stream_index];
444                 uint8_t language[4];
445                 url_fskip(pb, 12);
446                 get_buffer(pb, language, 3);
447                 if (language[0]) {
448                     language[3] = 0;
449                     av_metadata_set2(&st->metadata, "language", language, 0);
450                 }
451                 consumed += 15;
452             }
453         } else if (!ff_guidcmp(g, timestamp_guid)) {
454             int stream_index = ff_find_stream_index(s, sid);
455             if (stream_index >= 0) {
456                 url_fskip(pb, 8);
457                 wtv->pts = get_le64(pb);
458                 consumed += 16;
459                 if (wtv->pts == -1)
460                     wtv->pts = AV_NOPTS_VALUE;
461                 if (mode == SEEK_TO_PTS && wtv->pts >= seekts) {
462 #define WTV_PAD8(x) (((x) + 7) & ~7)
463                     url_fskip(pb, WTV_PAD8(len) - consumed);
464                     return 0;
465                 }
466             }
467         } else if (!ff_guidcmp(g, data_guid)) {
468             int stream_index = ff_find_stream_index(s, sid);
469             if (mode == SEEK_TO_DATA && stream_index >= 0) {
470                 WtvStream *wst = s->streams[stream_index]->priv_data;
471                 wst->seen_data = 1;
472                 if (len_ptr) {
473                     *len_ptr = len;
474                 }
475                 if (wtv->pts != AV_NOPTS_VALUE)
476                     av_add_index_entry(s->streams[stream_index], url_ftell(pb) - consumed, wtv->pts, 0, 0, AVINDEX_KEYFRAME);
477                 return stream_index;
478             }
479         } else if (
480             !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}) ||
481             !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}) ||
482             !ff_guidcmp(g, /* DSATTRIB_TRANSPORT_PROPERTIES */ (const ff_asf_guid){0x12,0xF6,0x22,0xB6,0xAD,0x47,0x71,0x46,0xAD,0x6C,0x05,0xA9,0x8E,0x65,0xDE,0x3A}) ||
483             !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}) ||
484             !ff_guidcmp(g, /* EVENTID_AudioTypeSpanningEvent */ (const ff_asf_guid){0xBE,0xBF,0x1C,0x50,0x49,0xB8,0xCE,0x42,0x9B,0xE9,0x3D,0xB8,0x69,0xFB,0x82,0xB3}) ||
485             !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}) ||
486             !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}) ||
487             !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}) ||
488             !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}) ||
489             !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}) ||
490             !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}) ||
491             !ff_guidcmp(g, (const ff_asf_guid){0x1E,0xBE,0xC3,0xC5,0x43,0x92,0xDC,0x11,0x85,0xE5,0x00,0x12,0x3F,0x6F,0x73,0xB9}) ||
492             !ff_guidcmp(g, (const ff_asf_guid){0x3B,0x86,0xA2,0xB1,0xEB,0x1E,0xC3,0x44,0x8C,0x88,0x1C,0xA3,0xFF,0xE3,0xE7,0x6A}) ||
493             !ff_guidcmp(g, (const ff_asf_guid){0x4E,0x7F,0x4C,0x5B,0xC4,0xD0,0x38,0x4B,0xA8,0x3E,0x21,0x7F,0x7B,0xBF,0x52,0xE7}) ||
494             !ff_guidcmp(g, (const ff_asf_guid){0x63,0x36,0xEB,0xFE,0xA1,0x7E,0xD9,0x11,0x83,0x08,0x00,0x07,0xE9,0x5E,0xAD,0x8D}) ||
495             !ff_guidcmp(g, (const ff_asf_guid){0x70,0xE9,0xF1,0xF8,0x89,0xA4,0x4C,0x4D,0x83,0x73,0xB8,0x12,0xE0,0xD5,0xF8,0x1E}) ||
496             !ff_guidcmp(g, (const ff_asf_guid){0x96,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D}) ||
497             !ff_guidcmp(g, (const ff_asf_guid){0x97,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D}) ||
498             !ff_guidcmp(g, (const ff_asf_guid){0xA1,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D})) {
499             //ignore known guids
500         } else
501             av_log(s, AV_LOG_WARNING, "unsupported chunk:"PRI_GUID"\n", ARG_GUID(g));
502
503         url_fskip(pb, WTV_PAD8(len) - consumed);
504     }
505     return AVERROR_EOF;
506 }
507
508 #define WTV_CHUNK_START 0x40000
509
510 static int read_header(AVFormatContext *s, AVFormatParameters *ap)
511 {
512     ByteIOContext *pb = s->pb;
513     int ret;
514
515     url_fseek(pb, WTV_CHUNK_START, SEEK_SET);
516     ret = parse_chunks(s, SEEK_TO_DATA, 0, 0);
517     if (ret < 0)
518         return ret;
519
520     url_fseek(pb, -32, SEEK_CUR);
521     return 0;
522 }
523
524 static int read_packet(AVFormatContext *s, AVPacket *pkt)
525 {
526     WtvContext *wtv = s->priv_data;
527     ByteIOContext *pb = s->pb;
528     int stream_index, len, ret;
529
530     stream_index = parse_chunks(s, SEEK_TO_DATA, 0, &len);
531     if (stream_index < 0)
532         return stream_index;
533
534     ret = av_get_packet(pb, pkt, len - 32);
535     if (ret < 0)
536         return ret;
537     pkt->stream_index = stream_index;
538     pkt->pts          = wtv->pts;
539     url_fskip(pb, WTV_PAD8(len) - len);
540     return 0;
541 }
542
543 static int read_seek2(AVFormatContext *s, int stream_index,
544                       int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
545 {
546     WtvContext *wtv = s->priv_data;
547     ByteIOContext *pb = s->pb;
548     AVStream *st;
549     int i;
550
551     if (stream_index < 0) {
552         stream_index = av_find_default_stream_index(s);
553         if (stream_index < 0)
554             return -1;
555     }
556     st = s->streams[stream_index];
557
558     if ((flags & AVSEEK_FLAG_FRAME)) {
559         return AVERROR_NOTSUPP;
560     } else if ((flags & AVSEEK_FLAG_BYTE)) {
561         if (ts < url_ftell(pb)) {
562             for (i = st->nb_index_entries - 1; i >= 0; i--) {
563                 if (st->index_entries[i].pos <= ts) {
564                     wtv->pts    = st->index_entries[i].timestamp;
565                     url_fseek(pb, st->index_entries[i].pos, SEEK_SET);
566                     break;
567                 }
568             }
569             if (i < 0) {
570                 wtv->pts = 0;
571                 url_fseek(pb, WTV_CHUNK_START, SEEK_SET);
572             }
573         }
574         if (parse_chunks(s, SEEK_TO_BYTE, ts, 0) < 0)
575             return AVERROR(ERANGE);
576         return 0;
577     } else {
578         ts *= 10;
579         i = av_index_search_timestamp(st, ts, flags);
580         if (i < 0) {
581             if (st->nb_index_entries > 0) {
582                 wtv->pts    = st->index_entries[st->nb_index_entries - 1].timestamp;
583                 url_fseek(pb, st->index_entries[st->nb_index_entries - 1].pos, SEEK_SET);
584             } else {
585                 wtv->pts = 0;
586                 url_fseek(pb, WTV_CHUNK_START, SEEK_SET);
587             }
588             if (parse_chunks(s, SEEK_TO_PTS, ts, 0) < 0)
589                 return AVERROR(ERANGE);
590             return 0;
591         }
592         wtv->pts    = st->index_entries[i].timestamp;
593         url_fseek(pb, st->index_entries[i].pos, SEEK_SET);
594         return 0;
595     }
596 }
597
598 AVInputFormat wtv_demuxer = {
599     .name           = "wtv",
600     .long_name      = NULL_IF_CONFIG_SMALL("Windows Television (WTV)"),
601     .priv_data_size = sizeof(WtvContext),
602     .read_probe     = read_probe,
603     .read_header    = read_header,
604     .read_packet    = read_packet,
605     .read_seek2     = read_seek2,
606     .flags          = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
607 };