Libav 0.7.1
libavformat/flvdec.c
Go to the documentation of this file.
00001 /*
00002  * FLV demuxer
00003  * Copyright (c) 2003 The Libav Project
00004  *
00005  * This demuxer will generate a 1 byte extradata for VP6F content.
00006  * It is composed of:
00007  *  - upper 4bits: difference between encoded width and visible width
00008  *  - lower 4bits: difference between encoded height and visible height
00009  *
00010  * This file is part of Libav.
00011  *
00012  * Libav is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU Lesser General Public
00014  * License as published by the Free Software Foundation; either
00015  * version 2.1 of the License, or (at your option) any later version.
00016  *
00017  * Libav is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * Lesser General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU Lesser General Public
00023  * License along with Libav; if not, write to the Free Software
00024  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00025  */
00026 
00027 #include "libavutil/avstring.h"
00028 #include "libavutil/dict.h"
00029 #include "libavcodec/bytestream.h"
00030 #include "libavcodec/mpeg4audio.h"
00031 #include "avformat.h"
00032 #include "avio_internal.h"
00033 #include "flv.h"
00034 
00035 #define KEYFRAMES_TAG            "keyframes"
00036 #define KEYFRAMES_TIMESTAMP_TAG  "times"
00037 #define KEYFRAMES_BYTEOFFSET_TAG "filepositions"
00038 
00039 typedef struct {
00040     int wrong_dts; 
00041 } FLVContext;
00042 
00043 static int flv_probe(AVProbeData *p)
00044 {
00045     const uint8_t *d;
00046 
00047     d = p->buf;
00048     if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
00049         return AVPROBE_SCORE_MAX;
00050     }
00051     return 0;
00052 }
00053 
00054 static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, int flv_codecid) {
00055     AVCodecContext *acodec = astream->codec;
00056     switch(flv_codecid) {
00057         //no distinction between S16 and S8 PCM codec flags
00058         case FLV_CODECID_PCM:
00059             acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 :
00060 #if HAVE_BIGENDIAN
00061                                 CODEC_ID_PCM_S16BE;
00062 #else
00063                                 CODEC_ID_PCM_S16LE;
00064 #endif
00065             break;
00066         case FLV_CODECID_PCM_LE:
00067             acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 : CODEC_ID_PCM_S16LE; break;
00068         case FLV_CODECID_AAC  : acodec->codec_id = CODEC_ID_AAC;                                    break;
00069         case FLV_CODECID_ADPCM: acodec->codec_id = CODEC_ID_ADPCM_SWF;                              break;
00070         case FLV_CODECID_SPEEX:
00071             acodec->codec_id = CODEC_ID_SPEEX;
00072             acodec->sample_rate = 16000;
00073             break;
00074         case FLV_CODECID_MP3  : acodec->codec_id = CODEC_ID_MP3      ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
00075         case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
00076             acodec->sample_rate = 8000; //in case metadata does not otherwise declare samplerate
00077             acodec->codec_id = CODEC_ID_NELLYMOSER;
00078             break;
00079         case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
00080             acodec->sample_rate = 16000;
00081             acodec->codec_id = CODEC_ID_NELLYMOSER;
00082             break;
00083         case FLV_CODECID_NELLYMOSER:
00084             acodec->codec_id = CODEC_ID_NELLYMOSER;
00085             break;
00086         default:
00087             av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
00088             acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
00089     }
00090 }
00091 
00092 static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
00093     AVCodecContext *vcodec = vstream->codec;
00094     switch(flv_codecid) {
00095         case FLV_CODECID_H263  : vcodec->codec_id = CODEC_ID_FLV1   ; break;
00096         case FLV_CODECID_SCREEN: vcodec->codec_id = CODEC_ID_FLASHSV; break;
00097         case FLV_CODECID_SCREEN2: vcodec->codec_id = CODEC_ID_FLASHSV2; break;
00098         case FLV_CODECID_VP6   : vcodec->codec_id = CODEC_ID_VP6F   ;
00099         case FLV_CODECID_VP6A  :
00100             if(flv_codecid == FLV_CODECID_VP6A)
00101                 vcodec->codec_id = CODEC_ID_VP6A;
00102             if(vcodec->extradata_size != 1) {
00103                 vcodec->extradata_size = 1;
00104                 vcodec->extradata = av_malloc(1);
00105             }
00106             vcodec->extradata[0] = avio_r8(s->pb);
00107             return 1; // 1 byte body size adjustment for flv_read_packet()
00108         case FLV_CODECID_H264:
00109             vcodec->codec_id = CODEC_ID_H264;
00110             return 3; // not 4, reading packet type will consume one byte
00111         default:
00112             av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
00113             vcodec->codec_tag = flv_codecid;
00114     }
00115 
00116     return 0;
00117 }
00118 
00119 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize) {
00120     int length = avio_rb16(ioc);
00121     if(length >= buffsize) {
00122         avio_skip(ioc, length);
00123         return -1;
00124     }
00125 
00126     avio_read(ioc, buffer, length);
00127 
00128     buffer[length] = '\0';
00129 
00130     return length;
00131 }
00132 
00133 static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, AVStream *vstream, int64_t max_pos) {
00134     unsigned int arraylen = 0, timeslen = 0, fileposlen = 0, i;
00135     double num_val;
00136     char str_val[256];
00137     int64_t *times = NULL;
00138     int64_t *filepositions = NULL;
00139     int ret = AVERROR(ENOSYS);
00140     int64_t initial_pos = avio_tell(ioc);
00141 
00142     while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
00143         int64_t* current_array;
00144 
00145         // Expect array object in context
00146         if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
00147             break;
00148 
00149         arraylen = avio_rb32(ioc);
00150         if (arraylen >> 28)
00151             break;
00152 
00153         /*
00154          * Expect only 'times' or 'filepositions' sub-arrays in other case refuse to use such metadata
00155          * for indexing
00156          */
00157         if (!strcmp(KEYFRAMES_TIMESTAMP_TAG, str_val) && !times) {
00158             if (!(times = av_mallocz(sizeof(*times) * arraylen))) {
00159                 ret = AVERROR(ENOMEM);
00160                 goto finish;
00161             }
00162             timeslen = arraylen;
00163             current_array = times;
00164         } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) && !filepositions) {
00165             if (!(filepositions = av_mallocz(sizeof(*filepositions) * arraylen))) {
00166                 ret = AVERROR(ENOMEM);
00167                 goto finish;
00168             }
00169             fileposlen = arraylen;
00170             current_array = filepositions;
00171         } else // unexpected metatag inside keyframes, will not use such metadata for indexing
00172             break;
00173 
00174         for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
00175             if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
00176                 goto finish;
00177             num_val = av_int2dbl(avio_rb64(ioc));
00178             current_array[i] = num_val;
00179         }
00180         if (times && filepositions) {
00181             // All done, exiting at a position allowing amf_parse_object
00182             // to finish parsing the object
00183             ret = 0;
00184             break;
00185         }
00186     }
00187 
00188     if (timeslen == fileposlen)
00189          for(i = 0; i < arraylen; i++)
00190              av_add_index_entry(vstream, filepositions[i], times[i]*1000, 0, 0, AVINDEX_KEYFRAME);
00191     else
00192         av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
00193 
00194 finish:
00195     av_freep(&times);
00196     av_freep(&filepositions);
00197     // If we got unexpected data, but successfully reset back to
00198     // the start pos, the caller can continue parsing
00199     if (ret < 0 && avio_seek(ioc, initial_pos, SEEK_SET) > 0)
00200         return 0;
00201     return ret;
00202 }
00203 
00204 static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
00205     AVCodecContext *acodec, *vcodec;
00206     AVIOContext *ioc;
00207     AMFDataType amf_type;
00208     char str_val[256];
00209     double num_val;
00210 
00211     num_val = 0;
00212     ioc = s->pb;
00213 
00214     amf_type = avio_r8(ioc);
00215 
00216     switch(amf_type) {
00217         case AMF_DATA_TYPE_NUMBER:
00218             num_val = av_int2dbl(avio_rb64(ioc)); break;
00219         case AMF_DATA_TYPE_BOOL:
00220             num_val = avio_r8(ioc); break;
00221         case AMF_DATA_TYPE_STRING:
00222             if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
00223                 return -1;
00224             break;
00225         case AMF_DATA_TYPE_OBJECT: {
00226             unsigned int keylen;
00227 
00228             if (key && !strcmp(KEYFRAMES_TAG, key) && depth == 1)
00229                 if (parse_keyframes_index(s, ioc, vstream, max_pos) < 0)
00230                     return -1;
00231 
00232             while(avio_tell(ioc) < max_pos - 2 && (keylen = avio_rb16(ioc))) {
00233                 avio_skip(ioc, keylen); //skip key string
00234                 if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
00235                     return -1; //if we couldn't skip, bomb out.
00236             }
00237             if(avio_r8(ioc) != AMF_END_OF_OBJECT)
00238                 return -1;
00239         }
00240             break;
00241         case AMF_DATA_TYPE_NULL:
00242         case AMF_DATA_TYPE_UNDEFINED:
00243         case AMF_DATA_TYPE_UNSUPPORTED:
00244             break; //these take up no additional space
00245         case AMF_DATA_TYPE_MIXEDARRAY:
00246             avio_skip(ioc, 4); //skip 32-bit max array index
00247             while(avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
00248                 //this is the only case in which we would want a nested parse to not skip over the object
00249                 if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
00250                     return -1;
00251             }
00252             if(avio_r8(ioc) != AMF_END_OF_OBJECT)
00253                 return -1;
00254             break;
00255         case AMF_DATA_TYPE_ARRAY: {
00256             unsigned int arraylen, i;
00257 
00258             arraylen = avio_rb32(ioc);
00259             for(i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
00260                 if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
00261                     return -1; //if we couldn't skip, bomb out.
00262             }
00263         }
00264             break;
00265         case AMF_DATA_TYPE_DATE:
00266             avio_skip(ioc, 8 + 2); //timestamp (double) and UTC offset (int16)
00267             break;
00268         default: //unsupported type, we couldn't skip
00269             return -1;
00270     }
00271 
00272     if(depth == 1 && key) { //only look for metadata values when we are not nested and key != NULL
00273         acodec = astream ? astream->codec : NULL;
00274         vcodec = vstream ? vstream->codec : NULL;
00275 
00276         if(amf_type == AMF_DATA_TYPE_BOOL) {
00277             av_strlcpy(str_val, num_val > 0 ? "true" : "false", sizeof(str_val));
00278             av_dict_set(&s->metadata, key, str_val, 0);
00279         } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
00280             snprintf(str_val, sizeof(str_val), "%.f", num_val);
00281             av_dict_set(&s->metadata, key, str_val, 0);
00282             if(!strcmp(key, "duration")) s->duration = num_val * AV_TIME_BASE;
00283             else if(!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
00284                 vcodec->bit_rate = num_val * 1024.0;
00285             else if(!strcmp(key, "audiodatarate") && acodec && 0 <= (int)(num_val * 1024.0))
00286                 acodec->bit_rate = num_val * 1024.0;
00287         } else if (amf_type == AMF_DATA_TYPE_STRING)
00288             av_dict_set(&s->metadata, key, str_val, 0);
00289     }
00290 
00291     return 0;
00292 }
00293 
00294 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
00295     AMFDataType type;
00296     AVStream *stream, *astream, *vstream;
00297     AVIOContext *ioc;
00298     int i;
00299     char buffer[11]; //only needs to hold the string "onMetaData". Anything longer is something we don't want.
00300 
00301     astream = NULL;
00302     vstream = NULL;
00303     ioc = s->pb;
00304 
00305     //first object needs to be "onMetaData" string
00306     type = avio_r8(ioc);
00307     if(type != AMF_DATA_TYPE_STRING || amf_get_string(ioc, buffer, sizeof(buffer)) < 0 || strcmp(buffer, "onMetaData"))
00308         return -1;
00309 
00310     //find the streams now so that amf_parse_object doesn't need to do the lookup every time it is called.
00311     for(i = 0; i < s->nb_streams; i++) {
00312         stream = s->streams[i];
00313         if     (stream->codec->codec_type == AVMEDIA_TYPE_AUDIO) astream = stream;
00314         else if(stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) vstream = stream;
00315     }
00316 
00317     //parse the second object (we want a mixed array)
00318     if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
00319         return -1;
00320 
00321     return 0;
00322 }
00323 
00324 static AVStream *create_stream(AVFormatContext *s, int is_audio){
00325     AVStream *st = av_new_stream(s, is_audio);
00326     if (!st)
00327         return NULL;
00328     st->codec->codec_type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
00329     av_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
00330     return st;
00331 }
00332 
00333 static int flv_read_header(AVFormatContext *s,
00334                            AVFormatParameters *ap)
00335 {
00336     int offset, flags;
00337 
00338     avio_skip(s->pb, 4);
00339     flags = avio_r8(s->pb);
00340     /* old flvtool cleared this field */
00341     /* FIXME: better fix needed */
00342     if (!flags) {
00343         flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
00344         av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
00345     }
00346 
00347     if((flags & (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
00348              != (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
00349         s->ctx_flags |= AVFMTCTX_NOHEADER;
00350 
00351     if(flags & FLV_HEADER_FLAG_HASVIDEO){
00352         if(!create_stream(s, 0))
00353             return AVERROR(ENOMEM);
00354     }
00355     if(flags & FLV_HEADER_FLAG_HASAUDIO){
00356         if(!create_stream(s, 1))
00357             return AVERROR(ENOMEM);
00358     }
00359 
00360     offset = avio_rb32(s->pb);
00361     avio_seek(s->pb, offset, SEEK_SET);
00362     avio_skip(s->pb, 4);
00363 
00364     s->start_time = 0;
00365 
00366     return 0;
00367 }
00368 
00369 static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
00370 {
00371     av_free(st->codec->extradata);
00372     st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
00373     if (!st->codec->extradata)
00374         return AVERROR(ENOMEM);
00375     st->codec->extradata_size = size;
00376     avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
00377     return 0;
00378 }
00379 
00380 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
00381 {
00382     FLVContext *flv = s->priv_data;
00383     int ret, i, type, size, flags, is_audio;
00384     int64_t next, pos;
00385     int64_t dts, pts = AV_NOPTS_VALUE;
00386     AVStream *st = NULL;
00387 
00388  for(;;avio_skip(s->pb, 4)){ /* pkt size is repeated at end. skip it */
00389     pos = avio_tell(s->pb);
00390     type = avio_r8(s->pb);
00391     size = avio_rb24(s->pb);
00392     dts = avio_rb24(s->pb);
00393     dts |= avio_r8(s->pb) << 24;
00394     av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
00395     if (s->pb->eof_reached)
00396         return AVERROR_EOF;
00397     avio_skip(s->pb, 3); /* stream id, always 0 */
00398     flags = 0;
00399 
00400     if(size == 0)
00401         continue;
00402 
00403     next= size + avio_tell(s->pb);
00404 
00405     if (type == FLV_TAG_TYPE_AUDIO) {
00406         is_audio=1;
00407         flags = avio_r8(s->pb);
00408         size--;
00409     } else if (type == FLV_TAG_TYPE_VIDEO) {
00410         is_audio=0;
00411         flags = avio_r8(s->pb);
00412         size--;
00413         if ((flags & 0xf0) == 0x50) /* video info / command frame */
00414             goto skip;
00415     } else {
00416         if (type == FLV_TAG_TYPE_META && size > 13+1+4)
00417             flv_read_metabody(s, next);
00418         else /* skip packet */
00419             av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
00420     skip:
00421         avio_seek(s->pb, next, SEEK_SET);
00422         continue;
00423     }
00424 
00425     /* skip empty data packets */
00426     if (!size)
00427         continue;
00428 
00429     /* now find stream */
00430     for(i=0;i<s->nb_streams;i++) {
00431         st = s->streams[i];
00432         if (st->id == is_audio)
00433             break;
00434     }
00435     if(i == s->nb_streams){
00436         av_log(s, AV_LOG_ERROR, "invalid stream\n");
00437         st= create_stream(s, is_audio);
00438         s->ctx_flags &= ~AVFMTCTX_NOHEADER;
00439     }
00440     av_dlog(s, "%d %X %d \n", is_audio, flags, st->discard);
00441     if(  (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||         is_audio))
00442        ||(st->discard >= AVDISCARD_BIDIR  &&  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
00443        || st->discard >= AVDISCARD_ALL
00444        ){
00445         avio_seek(s->pb, next, SEEK_SET);
00446         continue;
00447     }
00448     if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
00449         av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
00450     break;
00451  }
00452 
00453     // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps
00454     if(s->pb->seekable && (!s->duration || s->duration==AV_NOPTS_VALUE)){
00455         int size;
00456         const int64_t pos= avio_tell(s->pb);
00457         const int64_t fsize= avio_size(s->pb);
00458         avio_seek(s->pb, fsize-4, SEEK_SET);
00459         size= avio_rb32(s->pb);
00460         avio_seek(s->pb, fsize-3-size, SEEK_SET);
00461         if(size == avio_rb24(s->pb) + 11){
00462             uint32_t ts = avio_rb24(s->pb);
00463             ts |= avio_r8(s->pb) << 24;
00464             s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
00465         }
00466         avio_seek(s->pb, pos, SEEK_SET);
00467     }
00468 
00469     if(is_audio){
00470         if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
00471             st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
00472             st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
00473             st->codec->bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
00474         }
00475         if(!st->codec->codec_id){
00476             flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);
00477         }
00478     }else{
00479         size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
00480     }
00481 
00482     if (st->codec->codec_id == CODEC_ID_AAC ||
00483         st->codec->codec_id == CODEC_ID_H264) {
00484         int type = avio_r8(s->pb);
00485         size--;
00486         if (st->codec->codec_id == CODEC_ID_H264) {
00487             int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000; // sign extension
00488             pts = dts + cts;
00489             if (cts < 0) { // dts are wrong
00490                 flv->wrong_dts = 1;
00491                 av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
00492             }
00493             if (flv->wrong_dts)
00494                 dts = AV_NOPTS_VALUE;
00495         }
00496         if (type == 0) {
00497             if ((ret = flv_get_extradata(s, st, size)) < 0)
00498                 return ret;
00499             if (st->codec->codec_id == CODEC_ID_AAC) {
00500                 MPEG4AudioConfig cfg;
00501                 ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
00502                                          st->codec->extradata_size);
00503                 st->codec->channels = cfg.channels;
00504                 if (cfg.ext_sample_rate)
00505                     st->codec->sample_rate = cfg.ext_sample_rate;
00506                 else
00507                     st->codec->sample_rate = cfg.sample_rate;
00508                 av_dlog(s, "mp4a config channels %d sample rate %d\n",
00509                         st->codec->channels, st->codec->sample_rate);
00510             }
00511 
00512             ret = AVERROR(EAGAIN);
00513             goto leave;
00514         }
00515     }
00516 
00517     /* skip empty data packets */
00518     if (!size) {
00519         ret = AVERROR(EAGAIN);
00520         goto leave;
00521     }
00522 
00523     ret= av_get_packet(s->pb, pkt, size);
00524     if (ret < 0) {
00525         return AVERROR(EIO);
00526     }
00527     /* note: we need to modify the packet size here to handle the last
00528        packet */
00529     pkt->size = ret;
00530     pkt->dts = dts;
00531     pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
00532     pkt->stream_index = st->index;
00533 
00534     if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
00535         pkt->flags |= AV_PKT_FLAG_KEY;
00536 
00537 leave:
00538     avio_skip(s->pb, 4);
00539     return ret;
00540 }
00541 
00542 static int flv_read_seek(AVFormatContext *s, int stream_index,
00543     int64_t ts, int flags)
00544 {
00545     return avio_seek_time(s->pb, stream_index, ts, flags);
00546 }
00547 
00548 #if 0 /* don't know enough to implement this */
00549 static int flv_read_seek2(AVFormatContext *s, int stream_index,
00550     int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
00551 {
00552     int ret = AVERROR(ENOSYS);
00553 
00554     if (ts - min_ts > (uint64_t)(max_ts - ts)) flags |= AVSEEK_FLAG_BACKWARD;
00555 
00556     if (!s->pb->seekable) {
00557         if (stream_index < 0) {
00558             stream_index = av_find_default_stream_index(s);
00559             if (stream_index < 0)
00560                 return -1;
00561 
00562             /* timestamp for default must be expressed in AV_TIME_BASE units */
00563             ts = av_rescale_rnd(ts, 1000, AV_TIME_BASE,
00564                 flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
00565         }
00566         ret = avio_seek_time(s->pb, stream_index, ts, flags);
00567     }
00568 
00569     if (ret == AVERROR(ENOSYS))
00570         ret = av_seek_frame(s, stream_index, ts, flags);
00571     return ret;
00572 }
00573 #endif
00574 
00575 AVInputFormat ff_flv_demuxer = {
00576     "flv",
00577     NULL_IF_CONFIG_SMALL("FLV format"),
00578     sizeof(FLVContext),
00579     flv_probe,
00580     flv_read_header,
00581     flv_read_packet,
00582     .read_seek = flv_read_seek,
00583 #if 0
00584     .read_seek2 = flv_read_seek2,
00585 #endif
00586     .extensions = "flv",
00587     .value = CODEC_ID_FLV1,
00588 };