Mercurial > libavformat.hg
comparison flvdec.c @ 2771:d52c718e83f9 libavformat
Use dynamically allocated ByteIOContext in AVFormatContext
patch by: Bj«Órn Axelsson, bjorn d axelsson a intinor d se
thread: [PATCH] Remove static ByteIOContexts, 06 nov 2007
author | andoma |
---|---|
date | Wed, 21 Nov 2007 07:41:00 +0000 |
parents | 8258fb5a6aa0 |
children | f2a69a8c657d |
comparison
equal
deleted
inserted
replaced
2770:a7e42cf4b364 | 2771:d52c718e83f9 |
---|---|
69 vcodec->codec_id = CODEC_ID_VP6A; | 69 vcodec->codec_id = CODEC_ID_VP6A; |
70 if(vcodec->extradata_size != 1) { | 70 if(vcodec->extradata_size != 1) { |
71 vcodec->extradata_size = 1; | 71 vcodec->extradata_size = 1; |
72 vcodec->extradata = av_malloc(1); | 72 vcodec->extradata = av_malloc(1); |
73 } | 73 } |
74 vcodec->extradata[0] = get_byte(&s->pb); | 74 vcodec->extradata[0] = get_byte(s->pb); |
75 return 1; // 1 byte body size adjustment for flv_read_packet() | 75 return 1; // 1 byte body size adjustment for flv_read_packet() |
76 default: | 76 default: |
77 av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid); | 77 av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid); |
78 vcodec->codec_tag = flv_codecid; | 78 vcodec->codec_tag = flv_codecid; |
79 } | 79 } |
101 AMFDataType amf_type; | 101 AMFDataType amf_type; |
102 char str_val[256]; | 102 char str_val[256]; |
103 double num_val; | 103 double num_val; |
104 | 104 |
105 num_val = 0; | 105 num_val = 0; |
106 ioc = &s->pb; | 106 ioc = s->pb; |
107 | 107 |
108 amf_type = get_byte(ioc); | 108 amf_type = get_byte(ioc); |
109 | 109 |
110 switch(amf_type) { | 110 switch(amf_type) { |
111 case AMF_DATA_TYPE_NUMBER: | 111 case AMF_DATA_TYPE_NUMBER: |
201 char buffer[11]; //only needs to hold the string "onMetaData". Anything longer is something we don't want. | 201 char buffer[11]; //only needs to hold the string "onMetaData". Anything longer is something we don't want. |
202 | 202 |
203 astream = NULL; | 203 astream = NULL; |
204 vstream = NULL; | 204 vstream = NULL; |
205 keylen = 0; | 205 keylen = 0; |
206 ioc = &s->pb; | 206 ioc = s->pb; |
207 | 207 |
208 //first object needs to be "onMetaData" string | 208 //first object needs to be "onMetaData" string |
209 type = get_byte(ioc); | 209 type = get_byte(ioc); |
210 if(type != AMF_DATA_TYPE_STRING || amf_get_string(ioc, buffer, sizeof(buffer)) < 0 || strcmp(buffer, "onMetaData")) | 210 if(type != AMF_DATA_TYPE_STRING || amf_get_string(ioc, buffer, sizeof(buffer)) < 0 || strcmp(buffer, "onMetaData")) |
211 return -1; | 211 return -1; |
236 static int flv_read_header(AVFormatContext *s, | 236 static int flv_read_header(AVFormatContext *s, |
237 AVFormatParameters *ap) | 237 AVFormatParameters *ap) |
238 { | 238 { |
239 int offset, flags; | 239 int offset, flags; |
240 | 240 |
241 url_fskip(&s->pb, 4); | 241 url_fskip(s->pb, 4); |
242 flags = get_byte(&s->pb); | 242 flags = get_byte(s->pb); |
243 /* old flvtool cleared this field */ | 243 /* old flvtool cleared this field */ |
244 /* FIXME: better fix needed */ | 244 /* FIXME: better fix needed */ |
245 if (!flags) { | 245 if (!flags) { |
246 flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO; | 246 flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO; |
247 av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n"); | 247 av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n"); |
254 if(flags & FLV_HEADER_FLAG_HASAUDIO){ | 254 if(flags & FLV_HEADER_FLAG_HASAUDIO){ |
255 if(!create_stream(s, 1)) | 255 if(!create_stream(s, 1)) |
256 return AVERROR(ENOMEM); | 256 return AVERROR(ENOMEM); |
257 } | 257 } |
258 | 258 |
259 offset = get_be32(&s->pb); | 259 offset = get_be32(s->pb); |
260 url_fseek(&s->pb, offset, SEEK_SET); | 260 url_fseek(s->pb, offset, SEEK_SET); |
261 | 261 |
262 s->start_time = 0; | 262 s->start_time = 0; |
263 | 263 |
264 return 0; | 264 return 0; |
265 } | 265 } |
268 { | 268 { |
269 int ret, i, type, size, pts, flags, is_audio, next, pos; | 269 int ret, i, type, size, pts, flags, is_audio, next, pos; |
270 AVStream *st = NULL; | 270 AVStream *st = NULL; |
271 | 271 |
272 for(;;){ | 272 for(;;){ |
273 pos = url_ftell(&s->pb); | 273 pos = url_ftell(s->pb); |
274 url_fskip(&s->pb, 4); /* size of previous packet */ | 274 url_fskip(s->pb, 4); /* size of previous packet */ |
275 type = get_byte(&s->pb); | 275 type = get_byte(s->pb); |
276 size = get_be24(&s->pb); | 276 size = get_be24(s->pb); |
277 pts = get_be24(&s->pb); | 277 pts = get_be24(s->pb); |
278 pts |= get_byte(&s->pb) << 24; | 278 pts |= get_byte(s->pb) << 24; |
279 // av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, pts:%d\n", type, size, pts); | 279 // av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, pts:%d\n", type, size, pts); |
280 if (url_feof(&s->pb)) | 280 if (url_feof(s->pb)) |
281 return AVERROR(EIO); | 281 return AVERROR(EIO); |
282 url_fskip(&s->pb, 3); /* stream id, always 0 */ | 282 url_fskip(s->pb, 3); /* stream id, always 0 */ |
283 flags = 0; | 283 flags = 0; |
284 | 284 |
285 if(size == 0) | 285 if(size == 0) |
286 continue; | 286 continue; |
287 | 287 |
288 next= size + url_ftell(&s->pb); | 288 next= size + url_ftell(s->pb); |
289 | 289 |
290 if (type == FLV_TAG_TYPE_AUDIO) { | 290 if (type == FLV_TAG_TYPE_AUDIO) { |
291 is_audio=1; | 291 is_audio=1; |
292 flags = get_byte(&s->pb); | 292 flags = get_byte(s->pb); |
293 } else if (type == FLV_TAG_TYPE_VIDEO) { | 293 } else if (type == FLV_TAG_TYPE_VIDEO) { |
294 is_audio=0; | 294 is_audio=0; |
295 flags = get_byte(&s->pb); | 295 flags = get_byte(s->pb); |
296 } else { | 296 } else { |
297 if (type == FLV_TAG_TYPE_META && size > 13+1+4) | 297 if (type == FLV_TAG_TYPE_META && size > 13+1+4) |
298 flv_read_metabody(s, next); | 298 flv_read_metabody(s, next); |
299 else /* skip packet */ | 299 else /* skip packet */ |
300 av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags); | 300 av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags); |
301 url_fseek(&s->pb, next, SEEK_SET); | 301 url_fseek(s->pb, next, SEEK_SET); |
302 continue; | 302 continue; |
303 } | 303 } |
304 | 304 |
305 /* now find stream */ | 305 /* now find stream */ |
306 for(i=0;i<s->nb_streams;i++) { | 306 for(i=0;i<s->nb_streams;i++) { |
315 // av_log(NULL, AV_LOG_DEBUG, "%d %X %d \n", is_audio, flags, st->discard); | 315 // av_log(NULL, AV_LOG_DEBUG, "%d %X %d \n", is_audio, flags, st->discard); |
316 if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio)) | 316 if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio)) |
317 ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio)) | 317 ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio)) |
318 || st->discard >= AVDISCARD_ALL | 318 || st->discard >= AVDISCARD_ALL |
319 ){ | 319 ){ |
320 url_fseek(&s->pb, next, SEEK_SET); | 320 url_fseek(s->pb, next, SEEK_SET); |
321 continue; | 321 continue; |
322 } | 322 } |
323 if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY) | 323 if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY) |
324 av_add_index_entry(st, pos, pts, size, 0, AVINDEX_KEYFRAME); | 324 av_add_index_entry(st, pos, pts, size, 0, AVINDEX_KEYFRAME); |
325 break; | 325 break; |
326 } | 326 } |
327 | 327 |
328 // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps | 328 // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps |
329 if(!url_is_streamed(&s->pb) && s->duration==AV_NOPTS_VALUE){ | 329 if(!url_is_streamed(s->pb) && s->duration==AV_NOPTS_VALUE){ |
330 int size; | 330 int size; |
331 const int pos= url_ftell(&s->pb); | 331 const int pos= url_ftell(s->pb); |
332 const int fsize= url_fsize(&s->pb); | 332 const int fsize= url_fsize(s->pb); |
333 url_fseek(&s->pb, fsize-4, SEEK_SET); | 333 url_fseek(s->pb, fsize-4, SEEK_SET); |
334 size= get_be32(&s->pb); | 334 size= get_be32(s->pb); |
335 url_fseek(&s->pb, fsize-3-size, SEEK_SET); | 335 url_fseek(s->pb, fsize-3-size, SEEK_SET); |
336 if(size == get_be24(&s->pb) + 11){ | 336 if(size == get_be24(s->pb) + 11){ |
337 s->duration= get_be24(&s->pb) * (int64_t)AV_TIME_BASE / 1000; | 337 s->duration= get_be24(s->pb) * (int64_t)AV_TIME_BASE / 1000; |
338 } | 338 } |
339 url_fseek(&s->pb, pos, SEEK_SET); | 339 url_fseek(s->pb, pos, SEEK_SET); |
340 } | 340 } |
341 | 341 |
342 if(is_audio){ | 342 if(is_audio){ |
343 if(!st->codec->sample_rate || !st->codec->bits_per_sample || (!st->codec->codec_id && !st->codec->codec_tag)) { | 343 if(!st->codec->sample_rate || !st->codec->bits_per_sample || (!st->codec->codec_id && !st->codec->codec_tag)) { |
344 st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1; | 344 st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1; |
351 } | 351 } |
352 }else{ | 352 }else{ |
353 size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK); | 353 size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK); |
354 } | 354 } |
355 | 355 |
356 ret= av_get_packet(&s->pb, pkt, size - 1); | 356 ret= av_get_packet(s->pb, pkt, size - 1); |
357 if (ret <= 0) { | 357 if (ret <= 0) { |
358 return AVERROR(EIO); | 358 return AVERROR(EIO); |
359 } | 359 } |
360 /* note: we need to modify the packet size here to handle the last | 360 /* note: we need to modify the packet size here to handle the last |
361 packet */ | 361 packet */ |
378 { | 378 { |
379 AVStream *st = s->streams[stream_index]; | 379 AVStream *st = s->streams[stream_index]; |
380 int index = av_index_search_timestamp(st, timestamp, flags); | 380 int index = av_index_search_timestamp(st, timestamp, flags); |
381 if (index < 0) | 381 if (index < 0) |
382 return -1; | 382 return -1; |
383 url_fseek(&s->pb, st->index_entries[index].pos, SEEK_SET); | 383 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET); |
384 | 384 |
385 return 0; | 385 return 0; |
386 } | 386 } |
387 | 387 |
388 AVInputFormat flv_demuxer = { | 388 AVInputFormat flv_demuxer = { |