mov: Add support for zero-sized stsc runs.
[libav.git] / libavformat / flvdec.c
CommitLineData
d4f5d74a 1/*
7fbde343 2 * FLV demuxer
2912e87a 3 * Copyright (c) 2003 The Libav Project
d4f5d74a 4 *
7b94177e
DB
5 * This demuxer will generate a 1 byte extradata for VP6F content.
6 * It is composed of:
7 * - upper 4bits: difference between encoded width and visible width
8 * - lower 4bits: difference between encoded height and visible height
9 *
2912e87a 10 * This file is part of Libav.
b78e7197 11 *
2912e87a 12 * Libav is free software; you can redistribute it and/or
d4f5d74a
GM
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
b78e7197 15 * version 2.1 of the License, or (at your option) any later version.
d4f5d74a 16 *
2912e87a 17 * Libav is distributed in the hope that it will be useful,
d4f5d74a
GM
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
2912e87a 23 * License along with Libav; if not, write to the Free Software
5509bffa 24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d4f5d74a 25 */
d2718187 26
df2bd71a 27#include "libavutil/avstring.h"
c1c206b3 28#include "libavcodec/bytestream.h"
d2718187 29#include "libavcodec/mpeg4audio.h"
d4f5d74a 30#include "avformat.h"
933e90a6 31#include "avio_internal.h"
6cac3a3b 32#include "flv.h"
d4f5d74a 33
ebd61055
BC
34typedef struct {
35 int wrong_dts; ///< wrong dts due to negative cts
36} FLVContext;
37
d4f5d74a
GM
38static int flv_probe(AVProbeData *p)
39{
40 const uint8_t *d;
41
d4f5d74a 42 d = p->buf;
37e34df5 43 if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
74248229 44 return AVPROBE_SCORE_MAX;
d4f5d74a
GM
45 }
46 return 0;
47}
48
428cc588
AH
49static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, int flv_codecid) {
50 AVCodecContext *acodec = astream->codec;
51 switch(flv_codecid) {
52 //no distinction between S16 and S8 PCM codec flags
44de39f9 53 case FLV_CODECID_PCM:
8e9efe43 54 acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 :
63613fe6 55#if HAVE_BIGENDIAN
58293e57
MN
56 CODEC_ID_PCM_S16BE;
57#else
58 CODEC_ID_PCM_S16LE;
59#endif
60 break;
428cc588 61 case FLV_CODECID_PCM_LE:
8e9efe43 62 acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 : CODEC_ID_PCM_S16LE; break;
04fd3e81 63 case FLV_CODECID_AAC : acodec->codec_id = CODEC_ID_AAC; break;
428cc588 64 case FLV_CODECID_ADPCM: acodec->codec_id = CODEC_ID_ADPCM_SWF; break;
f96d0eef
BC
65 case FLV_CODECID_SPEEX:
66 acodec->codec_id = CODEC_ID_SPEEX;
67 acodec->sample_rate = 16000;
68 break;
57004ff1 69 case FLV_CODECID_MP3 : acodec->codec_id = CODEC_ID_MP3 ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
88cb61bb 70 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
428cc588 71 acodec->sample_rate = 8000; //in case metadata does not otherwise declare samplerate
caa7ad5d
TF
72 acodec->codec_id = CODEC_ID_NELLYMOSER;
73 break;
74 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
75 acodec->sample_rate = 16000;
76 acodec->codec_id = CODEC_ID_NELLYMOSER;
77 break;
428cc588 78 case FLV_CODECID_NELLYMOSER:
636b13c5
BL
79 acodec->codec_id = CODEC_ID_NELLYMOSER;
80 break;
428cc588
AH
81 default:
82 av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
83 acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
84 }
85}
86
87static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
88 AVCodecContext *vcodec = vstream->codec;
89 switch(flv_codecid) {
90 case FLV_CODECID_H263 : vcodec->codec_id = CODEC_ID_FLV1 ; break;
91 case FLV_CODECID_SCREEN: vcodec->codec_id = CODEC_ID_FLASHSV; break;
0aa6a518 92 case FLV_CODECID_SCREEN2: vcodec->codec_id = CODEC_ID_FLASHSV2; break;
428cc588 93 case FLV_CODECID_VP6 : vcodec->codec_id = CODEC_ID_VP6F ;
99904603
AJ
94 case FLV_CODECID_VP6A :
95 if(flv_codecid == FLV_CODECID_VP6A)
96 vcodec->codec_id = CODEC_ID_VP6A;
428cc588
AH
97 if(vcodec->extradata_size != 1) {
98 vcodec->extradata_size = 1;
99 vcodec->extradata = av_malloc(1);
100 }
b7effd4e 101 vcodec->extradata[0] = avio_r8(s->pb);
428cc588 102 return 1; // 1 byte body size adjustment for flv_read_packet()
04fd3e81
BC
103 case FLV_CODECID_H264:
104 vcodec->codec_id = CODEC_ID_H264;
105 return 3; // not 4, reading packet type will consume one byte
428cc588
AH
106 default:
107 av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
108 vcodec->codec_tag = flv_codecid;
109 }
110
111 return 0;
112}
113
ae628ec1 114static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize) {
b7effd4e 115 int length = avio_rb16(ioc);
cc38e063 116 if(length >= buffsize) {
45a8a02a 117 avio_skip(ioc, length);
cc38e063
MN
118 return -1;
119 }
896bcd2e 120
b7effd4e 121 avio_read(ioc, buffer, length);
896bcd2e 122
cc38e063 123 buffer[length] = '\0';
896bcd2e 124
cc38e063 125 return length;
896bcd2e
MN
126}
127
4fe8a452 128static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
428cc588 129 AVCodecContext *acodec, *vcodec;
ae628ec1 130 AVIOContext *ioc;
428cc588 131 AMFDataType amf_type;
cc38e063 132 char str_val[256];
428cc588
AH
133 double num_val;
134
135 num_val = 0;
899681cd 136 ioc = s->pb;
428cc588 137
b7effd4e 138 amf_type = avio_r8(ioc);
428cc588
AH
139
140 switch(amf_type) {
141 case AMF_DATA_TYPE_NUMBER:
b7effd4e 142 num_val = av_int2dbl(avio_rb64(ioc)); break;
428cc588 143 case AMF_DATA_TYPE_BOOL:
b7effd4e 144 num_val = avio_r8(ioc); break;
428cc588 145 case AMF_DATA_TYPE_STRING:
cc38e063 146 if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
428cc588
AH
147 return -1;
148 break;
149 case AMF_DATA_TYPE_OBJECT: {
150 unsigned int keylen;
151
a2704c97 152 while(avio_tell(ioc) < max_pos - 2 && (keylen = avio_rb16(ioc))) {
45a8a02a 153 avio_skip(ioc, keylen); //skip key string
428cc588
AH
154 if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
155 return -1; //if we couldn't skip, bomb out.
156 }
b7effd4e 157 if(avio_r8(ioc) != AMF_END_OF_OBJECT)
428cc588
AH
158 return -1;
159 }
160 break;
161 case AMF_DATA_TYPE_NULL:
162 case AMF_DATA_TYPE_UNDEFINED:
163 case AMF_DATA_TYPE_UNSUPPORTED:
164 break; //these take up no additional space
165 case AMF_DATA_TYPE_MIXEDARRAY:
45a8a02a 166 avio_skip(ioc, 4); //skip 32-bit max array index
a2704c97 167 while(avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
428cc588 168 //this is the only case in which we would want a nested parse to not skip over the object
cc38e063 169 if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
428cc588
AH
170 return -1;
171 }
b7effd4e 172 if(avio_r8(ioc) != AMF_END_OF_OBJECT)
428cc588
AH
173 return -1;
174 break;
175 case AMF_DATA_TYPE_ARRAY: {
176 unsigned int arraylen, i;
177
b7effd4e 178 arraylen = avio_rb32(ioc);
a2704c97 179 for(i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
428cc588
AH
180 if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
181 return -1; //if we couldn't skip, bomb out.
182 }
183 }
184 break;
185 case AMF_DATA_TYPE_DATE:
45a8a02a 186 avio_skip(ioc, 8 + 2); //timestamp (double) and UTC offset (int16)
428cc588
AH
187 break;
188 default: //unsupported type, we couldn't skip
189 return -1;
190 }
191
192 if(depth == 1 && key) { //only look for metadata values when we are not nested and key != NULL
193 acodec = astream ? astream->codec : NULL;
194 vcodec = vstream ? vstream->codec : NULL;
195
196 if(amf_type == AMF_DATA_TYPE_BOOL) {
cc38e063 197 av_strlcpy(str_val, num_val > 0 ? "true" : "false", sizeof(str_val));
2ef6c124 198 av_metadata_set2(&s->metadata, key, str_val, 0);
428cc588 199 } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
cc38e063 200 snprintf(str_val, sizeof(str_val), "%.f", num_val);
2ef6c124 201 av_metadata_set2(&s->metadata, key, str_val, 0);
428cc588 202 if(!strcmp(key, "duration")) s->duration = num_val * AV_TIME_BASE;
9a354fe3
SK
203 else if(!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
204 vcodec->bit_rate = num_val * 1024.0;
7e939205
HC
205 else if(!strcmp(key, "audiodatarate") && acodec && 0 <= (int)(num_val * 1024.0))
206 acodec->bit_rate = num_val * 1024.0;
df2bd71a 207 } else if (amf_type == AMF_DATA_TYPE_STRING)
2ef6c124 208 av_metadata_set2(&s->metadata, key, str_val, 0);
428cc588
AH
209 }
210
211 return 0;
212}
213
4fe8a452 214static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
428cc588
AH
215 AMFDataType type;
216 AVStream *stream, *astream, *vstream;
ae628ec1 217 AVIOContext *ioc;
4eec2606 218 int i;
cc38e063 219 char buffer[11]; //only needs to hold the string "onMetaData". Anything longer is something we don't want.
428cc588
AH
220
221 astream = NULL;
222 vstream = NULL;
899681cd 223 ioc = s->pb;
428cc588
AH
224
225 //first object needs to be "onMetaData" string
b7effd4e 226 type = avio_r8(ioc);
cc38e063 227 if(type != AMF_DATA_TYPE_STRING || amf_get_string(ioc, buffer, sizeof(buffer)) < 0 || strcmp(buffer, "onMetaData"))
428cc588
AH
228 return -1;
229
230 //find the streams now so that amf_parse_object doesn't need to do the lookup every time it is called.
231 for(i = 0; i < s->nb_streams; i++) {
232 stream = s->streams[i];
72415b2a
SS
233 if (stream->codec->codec_type == AVMEDIA_TYPE_AUDIO) astream = stream;
234 else if(stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) vstream = stream;
428cc588
AH
235 }
236
237 //parse the second object (we want a mixed array)
cc38e063 238 if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
428cc588
AH
239 return -1;
240
241 return 0;
242}
243
6f910bcf
MN
244static AVStream *create_stream(AVFormatContext *s, int is_audio){
245 AVStream *st = av_new_stream(s, is_audio);
246 if (!st)
247 return NULL;
72415b2a 248 st->codec->codec_type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
19719bc6 249 av_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
6f910bcf
MN
250 return st;
251}
252
d4f5d74a
GM
253static int flv_read_header(AVFormatContext *s,
254 AVFormatParameters *ap)
255{
15f14fc7 256 int offset, flags;
d4f5d74a 257
45a8a02a 258 avio_skip(s->pb, 4);
b7effd4e 259 flags = avio_r8(s->pb);
031311cb
AB
260 /* old flvtool cleared this field */
261 /* FIXME: better fix needed */
262 if (!flags) {
263 flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
264 av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
265 }
d4f5d74a 266
b41497e9
MN
267 if((flags & (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
268 != (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
269 s->ctx_flags |= AVFMTCTX_NOHEADER;
270
4eb0c665 271 if(flags & FLV_HEADER_FLAG_HASVIDEO){
6f910bcf 272 if(!create_stream(s, 0))
769e10f0 273 return AVERROR(ENOMEM);
4eb0c665
MN
274 }
275 if(flags & FLV_HEADER_FLAG_HASAUDIO){
6f910bcf 276 if(!create_stream(s, 1))
769e10f0 277 return AVERROR(ENOMEM);
4eb0c665
MN
278 }
279
b7effd4e 280 offset = avio_rb32(s->pb);
6b4aa5da 281 avio_seek(s->pb, offset, SEEK_SET);
45a8a02a 282 avio_skip(s->pb, 4);
d4f5d74a 283
aeb20f7f
N
284 s->start_time = 0;
285
d4f5d74a
GM
286 return 0;
287}
288
04fd3e81
BC
289static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
290{
291 av_free(st->codec->extradata);
292 st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
293 if (!st->codec->extradata)
294 return AVERROR(ENOMEM);
295 st->codec->extradata_size = size;
b7effd4e 296 avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
04fd3e81
BC
297 return 0;
298}
299
d4f5d74a
GM
300static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
301{
ebd61055 302 FLVContext *flv = s->priv_data;
4fe8a452
PM
303 int ret, i, type, size, flags, is_audio;
304 int64_t next, pos;
ebd61055 305 int64_t dts, pts = AV_NOPTS_VALUE;
923bd441 306 AVStream *st = NULL;
115329f1 307
45a8a02a 308 for(;;avio_skip(s->pb, 4)){ /* pkt size is repeated at end. skip it */
a2704c97 309 pos = avio_tell(s->pb);
b7effd4e
AK
310 type = avio_r8(s->pb);
311 size = avio_rb24(s->pb);
312 dts = avio_rb24(s->pb);
313 dts |= avio_r8(s->pb) << 24;
7ef94d22 314// av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, dts:%d\n", type, size, dts);
66e5b1df 315 if (s->pb->eof_reached)
0e9602ef 316 return AVERROR_EOF;
45a8a02a 317 avio_skip(s->pb, 3); /* stream id, always 0 */
d4f5d74a 318 flags = 0;
115329f1 319
068f2a22
MN
320 if(size == 0)
321 continue;
115329f1 322
a2704c97 323 next= size + avio_tell(s->pb);
dd9f5916 324
6cac3a3b 325 if (type == FLV_TAG_TYPE_AUDIO) {
068f2a22 326 is_audio=1;
b7effd4e 327 flags = avio_r8(s->pb);
6298eb81 328 size--;
6cac3a3b 329 } else if (type == FLV_TAG_TYPE_VIDEO) {
068f2a22 330 is_audio=0;
b7effd4e 331 flags = avio_r8(s->pb);
6298eb81 332 size--;
3d9aecb0
BC
333 if ((flags & 0xf0) == 0x50) /* video info / command frame */
334 goto skip;
d4f5d74a 335 } else {
09e54e1f 336 if (type == FLV_TAG_TYPE_META && size > 13+1+4)
428cc588
AH
337 flv_read_metabody(s, next);
338 else /* skip packet */
e1d8d7bb 339 av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
3d9aecb0 340 skip:
6b4aa5da 341 avio_seek(s->pb, next, SEEK_SET);
ded01499 342 continue;
d4f5d74a
GM
343 }
344
ae58b54b
BC
345 /* skip empty data packets */
346 if (!size)
347 continue;
348
d4f5d74a
GM
349 /* now find stream */
350 for(i=0;i<s->nb_streams;i++) {
351 st = s->streams[i];
068f2a22
MN
352 if (st->id == is_audio)
353 break;
d4f5d74a 354 }
068f2a22 355 if(i == s->nb_streams){
c1023470 356 av_log(s, AV_LOG_ERROR, "invalid stream\n");
c8652b57 357 st= create_stream(s, is_audio);
a33cfa30 358 s->ctx_flags &= ~AVFMTCTX_NOHEADER;
6ed08157 359 }
c1023470 360// av_log(s, AV_LOG_DEBUG, "%d %X %d \n", is_audio, flags, st->discard);
6cac3a3b
AH
361 if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio))
362 ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
f3356e9c
MN
363 || st->discard >= AVDISCARD_ALL
364 ){
6b4aa5da 365 avio_seek(s->pb, next, SEEK_SET);
ded01499 366 continue;
b9866ebc 367 }
6cac3a3b 368 if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
7ef94d22 369 av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
068f2a22
MN
370 break;
371 }
372
15f14fc7 373 // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps
fdf46622 374 if(!url_is_streamed(s->pb) && (!s->duration || s->duration==AV_NOPTS_VALUE)){
15f14fc7 375 int size;
a2704c97 376 const int64_t pos= avio_tell(s->pb);
76aa876e 377 const int64_t fsize= avio_size(s->pb);
6b4aa5da 378 avio_seek(s->pb, fsize-4, SEEK_SET);
b7effd4e 379 size= avio_rb32(s->pb);
6b4aa5da 380 avio_seek(s->pb, fsize-3-size, SEEK_SET);
b7effd4e
AK
381 if(size == avio_rb24(s->pb) + 11){
382 uint32_t ts = avio_rb24(s->pb);
383 ts |= avio_r8(s->pb) << 24;
b126dee9 384 s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
15f14fc7 385 }
6b4aa5da 386 avio_seek(s->pb, pos, SEEK_SET);
15f14fc7
MN
387 }
388
068f2a22 389 if(is_audio){
2f3d7ea9 390 if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
6cac3a3b 391 st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
7f8cd075 392 st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
dd1c8f3e 393 st->codec->bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
2f3d7ea9
MN
394 }
395 if(!st->codec->codec_id){
428cc588 396 flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);
068f2a22
MN
397 }
398 }else{
428cc588 399 size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
bb01a3f0
MN
400 }
401
04fd3e81
BC
402 if (st->codec->codec_id == CODEC_ID_AAC ||
403 st->codec->codec_id == CODEC_ID_H264) {
b7effd4e 404 int type = avio_r8(s->pb);
04fd3e81
BC
405 size--;
406 if (st->codec->codec_id == CODEC_ID_H264) {
b7effd4e 407 int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000; // sign extension
ebd61055
BC
408 pts = dts + cts;
409 if (cts < 0) { // dts are wrong
410 flv->wrong_dts = 1;
411 av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
412 }
413 if (flv->wrong_dts)
414 dts = AV_NOPTS_VALUE;
04fd3e81
BC
415 }
416 if (type == 0) {
6298eb81 417 if ((ret = flv_get_extradata(s, st, size)) < 0)
04fd3e81 418 return ret;
d2718187
BC
419 if (st->codec->codec_id == CODEC_ID_AAC) {
420 MPEG4AudioConfig cfg;
421 ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
422 st->codec->extradata_size);
5aea268d 423 st->codec->channels = cfg.channels;
7d6096e4
BC
424 if (cfg.ext_sample_rate)
425 st->codec->sample_rate = cfg.ext_sample_rate;
426 else
427 st->codec->sample_rate = cfg.sample_rate;
dfd2a005 428 av_dlog(s, "mp4a config channels %d sample rate %d\n",
d2718187
BC
429 st->codec->channels, st->codec->sample_rate);
430 }
431
527c2e64
HC
432 ret = AVERROR(EAGAIN);
433 goto leave;
04fd3e81
BC
434 }
435 }
436
fcb4228c 437 /* skip empty data packets */
527c2e64
HC
438 if (!size) {
439 ret = AVERROR(EAGAIN);
440 goto leave;
441 }
fcb4228c 442
6298eb81 443 ret= av_get_packet(s->pb, pkt, size);
634b927f 444 if (ret < 0) {
6f3e0b21 445 return AVERROR(EIO);
d4f5d74a
GM
446 }
447 /* note: we need to modify the packet size here to handle the last
448 packet */
449 pkt->size = ret;
7ef94d22 450 pkt->dts = dts;
ebd61055 451 pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
d4f5d74a 452 pkt->stream_index = st->index;
115329f1 453
6cac3a3b 454 if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
cc947f04 455 pkt->flags |= AV_PKT_FLAG_KEY;
115329f1 456
527c2e64 457leave:
45a8a02a 458 avio_skip(s->pb, 4);
d4f5d74a
GM
459 return ret;
460}
461
fc8fa007
HC
462static int flv_read_seek(AVFormatContext *s, int stream_index,
463 int64_t ts, int flags)
464{
933e90a6 465 return ffio_read_seek(s->pb, stream_index, ts, flags);
fc8fa007
HC
466}
467
468#if 0 /* don't know enough to implement this */
469static int flv_read_seek2(AVFormatContext *s, int stream_index,
470 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
471{
472 int ret = AVERROR(ENOSYS);
473
474 if (ts - min_ts > (uint64_t)(max_ts - ts)) flags |= AVSEEK_FLAG_BACKWARD;
475
476 if (url_is_streamed(s->pb)) {
477 if (stream_index < 0) {
478 stream_index = av_find_default_stream_index(s);
479 if (stream_index < 0)
480 return -1;
481
482 /* timestamp for default must be expressed in AV_TIME_BASE units */
483 ts = av_rescale_rnd(ts, 1000, AV_TIME_BASE,
484 flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
485 }
933e90a6 486 ret = ffio_read_seek(s->pb, stream_index, ts, flags);
fc8fa007
HC
487 }
488
489 if (ret == AVERROR(ENOSYS))
490 ret = av_seek_frame(s, stream_index, ts, flags);
491 return ret;
492}
493#endif
494
c6610a21 495AVInputFormat ff_flv_demuxer = {
d4f5d74a 496 "flv",
bde15e74 497 NULL_IF_CONFIG_SMALL("FLV format"),
ebd61055 498 sizeof(FLVContext),
d4f5d74a
GM
499 flv_probe,
500 flv_read_header,
501 flv_read_packet,
fc8fa007
HC
502 .read_seek = flv_read_seek,
503#if 0
504 .read_seek2 = flv_read_seek2,
505#endif
d4f5d74a
GM
506 .extensions = "flv",
507 .value = CODEC_ID_FLV1,
508};