You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

587 lines
20KB

  1. /*
  2. * FLV demuxer
  3. * Copyright (c) 2003 The Libav Project
  4. *
  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. *
  10. * This file is part of Libav.
  11. *
  12. * Libav is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * Libav is distributed in the hope that it will be useful,
  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
  23. * License along with Libav; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/dict.h"
  28. #include "libavutil/intfloat_readwrite.h"
  29. #include "libavcodec/bytestream.h"
  30. #include "libavcodec/mpeg4audio.h"
  31. #include "avformat.h"
  32. #include "avio_internal.h"
  33. #include "flv.h"
  34. #define KEYFRAMES_TAG "keyframes"
  35. #define KEYFRAMES_TIMESTAMP_TAG "times"
  36. #define KEYFRAMES_BYTEOFFSET_TAG "filepositions"
  37. typedef struct {
  38. int wrong_dts; ///< wrong dts due to negative cts
  39. } FLVContext;
  40. static int flv_probe(AVProbeData *p)
  41. {
  42. const uint8_t *d;
  43. d = p->buf;
  44. if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
  45. return AVPROBE_SCORE_MAX;
  46. }
  47. return 0;
  48. }
  49. static 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
  53. case FLV_CODECID_PCM:
  54. acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 :
  55. #if HAVE_BIGENDIAN
  56. CODEC_ID_PCM_S16BE;
  57. #else
  58. CODEC_ID_PCM_S16LE;
  59. #endif
  60. break;
  61. case FLV_CODECID_PCM_LE:
  62. acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_U8 : CODEC_ID_PCM_S16LE; break;
  63. case FLV_CODECID_AAC : acodec->codec_id = CODEC_ID_AAC; break;
  64. case FLV_CODECID_ADPCM: acodec->codec_id = CODEC_ID_ADPCM_SWF; break;
  65. case FLV_CODECID_SPEEX:
  66. acodec->codec_id = CODEC_ID_SPEEX;
  67. acodec->sample_rate = 16000;
  68. break;
  69. case FLV_CODECID_MP3 : acodec->codec_id = CODEC_ID_MP3 ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
  70. case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
  71. acodec->sample_rate = 8000; //in case metadata does not otherwise declare samplerate
  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;
  78. case FLV_CODECID_NELLYMOSER:
  79. acodec->codec_id = CODEC_ID_NELLYMOSER;
  80. break;
  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. static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
  87. AVCodecContext *vcodec = vstream->codec;
  88. switch(flv_codecid) {
  89. case FLV_CODECID_H263 : vcodec->codec_id = CODEC_ID_FLV1 ; break;
  90. case FLV_CODECID_SCREEN: vcodec->codec_id = CODEC_ID_FLASHSV; break;
  91. case FLV_CODECID_SCREEN2: vcodec->codec_id = CODEC_ID_FLASHSV2; break;
  92. case FLV_CODECID_VP6 : vcodec->codec_id = CODEC_ID_VP6F ;
  93. case FLV_CODECID_VP6A :
  94. if(flv_codecid == FLV_CODECID_VP6A)
  95. vcodec->codec_id = CODEC_ID_VP6A;
  96. if(vcodec->extradata_size != 1) {
  97. vcodec->extradata_size = 1;
  98. vcodec->extradata = av_malloc(1);
  99. }
  100. vcodec->extradata[0] = avio_r8(s->pb);
  101. return 1; // 1 byte body size adjustment for flv_read_packet()
  102. case FLV_CODECID_H264:
  103. vcodec->codec_id = CODEC_ID_H264;
  104. return 3; // not 4, reading packet type will consume one byte
  105. default:
  106. av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
  107. vcodec->codec_tag = flv_codecid;
  108. }
  109. return 0;
  110. }
  111. static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize) {
  112. int length = avio_rb16(ioc);
  113. if(length >= buffsize) {
  114. avio_skip(ioc, length);
  115. return -1;
  116. }
  117. avio_read(ioc, buffer, length);
  118. buffer[length] = '\0';
  119. return length;
  120. }
  121. static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, AVStream *vstream, int64_t max_pos) {
  122. unsigned int arraylen = 0, timeslen = 0, fileposlen = 0, i;
  123. double num_val;
  124. char str_val[256];
  125. int64_t *times = NULL;
  126. int64_t *filepositions = NULL;
  127. int ret = AVERROR(ENOSYS);
  128. int64_t initial_pos = avio_tell(ioc);
  129. while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
  130. int64_t* current_array;
  131. // Expect array object in context
  132. if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
  133. break;
  134. arraylen = avio_rb32(ioc);
  135. /*
  136. * Expect only 'times' or 'filepositions' sub-arrays in other case refuse to use such metadata
  137. * for indexing
  138. */
  139. if (!strcmp(KEYFRAMES_TIMESTAMP_TAG, str_val) && !times) {
  140. if (!(times = av_mallocz(sizeof(*times) * arraylen))) {
  141. ret = AVERROR(ENOMEM);
  142. goto finish;
  143. }
  144. timeslen = arraylen;
  145. current_array = times;
  146. } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) && !filepositions) {
  147. if (!(filepositions = av_mallocz(sizeof(*filepositions) * arraylen))) {
  148. ret = AVERROR(ENOMEM);
  149. goto finish;
  150. }
  151. fileposlen = arraylen;
  152. current_array = filepositions;
  153. } else // unexpected metatag inside keyframes, will not use such metadata for indexing
  154. break;
  155. for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
  156. if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
  157. goto finish;
  158. num_val = av_int2dbl(avio_rb64(ioc));
  159. current_array[i] = num_val;
  160. }
  161. if (times && filepositions) {
  162. // All done, exiting at a position allowing amf_parse_object
  163. // to finish parsing the object
  164. ret = 0;
  165. break;
  166. }
  167. }
  168. if (timeslen == fileposlen)
  169. for(i = 0; i < arraylen; i++)
  170. av_add_index_entry(vstream, filepositions[i], times[i]*1000, 0, 0, AVINDEX_KEYFRAME);
  171. else
  172. av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
  173. finish:
  174. av_freep(&times);
  175. av_freep(&filepositions);
  176. // If we got unexpected data, but successfully reset back to
  177. // the start pos, the caller can continue parsing
  178. if (ret < 0 && avio_seek(ioc, initial_pos, SEEK_SET) > 0)
  179. return 0;
  180. return ret;
  181. }
  182. static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
  183. AVCodecContext *acodec, *vcodec;
  184. AVIOContext *ioc;
  185. AMFDataType amf_type;
  186. char str_val[256];
  187. double num_val;
  188. num_val = 0;
  189. ioc = s->pb;
  190. amf_type = avio_r8(ioc);
  191. switch(amf_type) {
  192. case AMF_DATA_TYPE_NUMBER:
  193. num_val = av_int2dbl(avio_rb64(ioc)); break;
  194. case AMF_DATA_TYPE_BOOL:
  195. num_val = avio_r8(ioc); break;
  196. case AMF_DATA_TYPE_STRING:
  197. if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
  198. return -1;
  199. break;
  200. case AMF_DATA_TYPE_OBJECT: {
  201. unsigned int keylen;
  202. if (key && !strcmp(KEYFRAMES_TAG, key) && depth == 1)
  203. if (parse_keyframes_index(s, ioc, vstream, max_pos) < 0)
  204. return -1;
  205. while(avio_tell(ioc) < max_pos - 2 && (keylen = avio_rb16(ioc))) {
  206. avio_skip(ioc, keylen); //skip key string
  207. if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
  208. return -1; //if we couldn't skip, bomb out.
  209. }
  210. if(avio_r8(ioc) != AMF_END_OF_OBJECT)
  211. return -1;
  212. }
  213. break;
  214. case AMF_DATA_TYPE_NULL:
  215. case AMF_DATA_TYPE_UNDEFINED:
  216. case AMF_DATA_TYPE_UNSUPPORTED:
  217. break; //these take up no additional space
  218. case AMF_DATA_TYPE_MIXEDARRAY:
  219. avio_skip(ioc, 4); //skip 32-bit max array index
  220. while(avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
  221. //this is the only case in which we would want a nested parse to not skip over the object
  222. if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
  223. return -1;
  224. }
  225. if(avio_r8(ioc) != AMF_END_OF_OBJECT)
  226. return -1;
  227. break;
  228. case AMF_DATA_TYPE_ARRAY: {
  229. unsigned int arraylen, i;
  230. arraylen = avio_rb32(ioc);
  231. for(i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
  232. if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
  233. return -1; //if we couldn't skip, bomb out.
  234. }
  235. }
  236. break;
  237. case AMF_DATA_TYPE_DATE:
  238. avio_skip(ioc, 8 + 2); //timestamp (double) and UTC offset (int16)
  239. break;
  240. default: //unsupported type, we couldn't skip
  241. return -1;
  242. }
  243. if(depth == 1 && key) { //only look for metadata values when we are not nested and key != NULL
  244. acodec = astream ? astream->codec : NULL;
  245. vcodec = vstream ? vstream->codec : NULL;
  246. if(amf_type == AMF_DATA_TYPE_BOOL) {
  247. av_strlcpy(str_val, num_val > 0 ? "true" : "false", sizeof(str_val));
  248. av_dict_set(&s->metadata, key, str_val, 0);
  249. } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
  250. snprintf(str_val, sizeof(str_val), "%.f", num_val);
  251. av_dict_set(&s->metadata, key, str_val, 0);
  252. if(!strcmp(key, "duration")) s->duration = num_val * AV_TIME_BASE;
  253. else if(!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
  254. vcodec->bit_rate = num_val * 1024.0;
  255. else if(!strcmp(key, "audiodatarate") && acodec && 0 <= (int)(num_val * 1024.0))
  256. acodec->bit_rate = num_val * 1024.0;
  257. } else if (amf_type == AMF_DATA_TYPE_STRING)
  258. av_dict_set(&s->metadata, key, str_val, 0);
  259. }
  260. return 0;
  261. }
  262. static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
  263. AMFDataType type;
  264. AVStream *stream, *astream, *vstream;
  265. AVIOContext *ioc;
  266. int i;
  267. char buffer[11]; //only needs to hold the string "onMetaData". Anything longer is something we don't want.
  268. astream = NULL;
  269. vstream = NULL;
  270. ioc = s->pb;
  271. //first object needs to be "onMetaData" string
  272. type = avio_r8(ioc);
  273. if(type != AMF_DATA_TYPE_STRING || amf_get_string(ioc, buffer, sizeof(buffer)) < 0 || strcmp(buffer, "onMetaData"))
  274. return -1;
  275. //find the streams now so that amf_parse_object doesn't need to do the lookup every time it is called.
  276. for(i = 0; i < s->nb_streams; i++) {
  277. stream = s->streams[i];
  278. if (stream->codec->codec_type == AVMEDIA_TYPE_AUDIO) astream = stream;
  279. else if(stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) vstream = stream;
  280. }
  281. //parse the second object (we want a mixed array)
  282. if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
  283. return -1;
  284. return 0;
  285. }
  286. static AVStream *create_stream(AVFormatContext *s, int is_audio){
  287. AVStream *st = av_new_stream(s, is_audio);
  288. if (!st)
  289. return NULL;
  290. st->codec->codec_type = is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO;
  291. av_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
  292. return st;
  293. }
  294. static int flv_read_header(AVFormatContext *s,
  295. AVFormatParameters *ap)
  296. {
  297. int offset, flags;
  298. avio_skip(s->pb, 4);
  299. flags = avio_r8(s->pb);
  300. /* old flvtool cleared this field */
  301. /* FIXME: better fix needed */
  302. if (!flags) {
  303. flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
  304. av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
  305. }
  306. if((flags & (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
  307. != (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
  308. s->ctx_flags |= AVFMTCTX_NOHEADER;
  309. if(flags & FLV_HEADER_FLAG_HASVIDEO){
  310. if(!create_stream(s, 0))
  311. return AVERROR(ENOMEM);
  312. }
  313. if(flags & FLV_HEADER_FLAG_HASAUDIO){
  314. if(!create_stream(s, 1))
  315. return AVERROR(ENOMEM);
  316. }
  317. offset = avio_rb32(s->pb);
  318. avio_seek(s->pb, offset, SEEK_SET);
  319. avio_skip(s->pb, 4);
  320. s->start_time = 0;
  321. return 0;
  322. }
  323. static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
  324. {
  325. av_free(st->codec->extradata);
  326. st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  327. if (!st->codec->extradata)
  328. return AVERROR(ENOMEM);
  329. st->codec->extradata_size = size;
  330. avio_read(s->pb, st->codec->extradata, st->codec->extradata_size);
  331. return 0;
  332. }
  333. static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
  334. {
  335. FLVContext *flv = s->priv_data;
  336. int ret, i, type, size, flags, is_audio;
  337. int64_t next, pos;
  338. int64_t dts, pts = AV_NOPTS_VALUE;
  339. AVStream *st = NULL;
  340. for(;;avio_skip(s->pb, 4)){ /* pkt size is repeated at end. skip it */
  341. pos = avio_tell(s->pb);
  342. type = avio_r8(s->pb);
  343. size = avio_rb24(s->pb);
  344. dts = avio_rb24(s->pb);
  345. dts |= avio_r8(s->pb) << 24;
  346. av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
  347. if (s->pb->eof_reached)
  348. return AVERROR_EOF;
  349. avio_skip(s->pb, 3); /* stream id, always 0 */
  350. flags = 0;
  351. if(size == 0)
  352. continue;
  353. next= size + avio_tell(s->pb);
  354. if (type == FLV_TAG_TYPE_AUDIO) {
  355. is_audio=1;
  356. flags = avio_r8(s->pb);
  357. size--;
  358. } else if (type == FLV_TAG_TYPE_VIDEO) {
  359. is_audio=0;
  360. flags = avio_r8(s->pb);
  361. size--;
  362. if ((flags & 0xf0) == 0x50) /* video info / command frame */
  363. goto skip;
  364. } else {
  365. if (type == FLV_TAG_TYPE_META && size > 13+1+4)
  366. flv_read_metabody(s, next);
  367. else /* skip packet */
  368. av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
  369. skip:
  370. avio_seek(s->pb, next, SEEK_SET);
  371. continue;
  372. }
  373. /* skip empty data packets */
  374. if (!size)
  375. continue;
  376. /* now find stream */
  377. for(i=0;i<s->nb_streams;i++) {
  378. st = s->streams[i];
  379. if (st->id == is_audio)
  380. break;
  381. }
  382. if(i == s->nb_streams){
  383. av_log(s, AV_LOG_ERROR, "invalid stream\n");
  384. st= create_stream(s, is_audio);
  385. s->ctx_flags &= ~AVFMTCTX_NOHEADER;
  386. }
  387. av_dlog(s, "%d %X %d \n", is_audio, flags, st->discard);
  388. if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio))
  389. ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
  390. || st->discard >= AVDISCARD_ALL
  391. ){
  392. avio_seek(s->pb, next, SEEK_SET);
  393. continue;
  394. }
  395. if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
  396. av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
  397. break;
  398. }
  399. // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps
  400. if(s->pb->seekable && (!s->duration || s->duration==AV_NOPTS_VALUE)){
  401. int size;
  402. const int64_t pos= avio_tell(s->pb);
  403. const int64_t fsize= avio_size(s->pb);
  404. avio_seek(s->pb, fsize-4, SEEK_SET);
  405. size= avio_rb32(s->pb);
  406. avio_seek(s->pb, fsize-3-size, SEEK_SET);
  407. if(size == avio_rb24(s->pb) + 11){
  408. uint32_t ts = avio_rb24(s->pb);
  409. ts |= avio_r8(s->pb) << 24;
  410. s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
  411. }
  412. avio_seek(s->pb, pos, SEEK_SET);
  413. }
  414. if(is_audio){
  415. if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
  416. st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
  417. st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
  418. st->codec->bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
  419. }
  420. if(!st->codec->codec_id){
  421. flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);
  422. }
  423. }else{
  424. size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
  425. }
  426. if (st->codec->codec_id == CODEC_ID_AAC ||
  427. st->codec->codec_id == CODEC_ID_H264) {
  428. int type = avio_r8(s->pb);
  429. size--;
  430. if (st->codec->codec_id == CODEC_ID_H264) {
  431. int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000; // sign extension
  432. pts = dts + cts;
  433. if (cts < 0) { // dts are wrong
  434. flv->wrong_dts = 1;
  435. av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
  436. }
  437. if (flv->wrong_dts)
  438. dts = AV_NOPTS_VALUE;
  439. }
  440. if (type == 0) {
  441. if ((ret = flv_get_extradata(s, st, size)) < 0)
  442. return ret;
  443. if (st->codec->codec_id == CODEC_ID_AAC) {
  444. MPEG4AudioConfig cfg;
  445. ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
  446. st->codec->extradata_size);
  447. st->codec->channels = cfg.channels;
  448. if (cfg.ext_sample_rate)
  449. st->codec->sample_rate = cfg.ext_sample_rate;
  450. else
  451. st->codec->sample_rate = cfg.sample_rate;
  452. av_dlog(s, "mp4a config channels %d sample rate %d\n",
  453. st->codec->channels, st->codec->sample_rate);
  454. }
  455. ret = AVERROR(EAGAIN);
  456. goto leave;
  457. }
  458. }
  459. /* skip empty data packets */
  460. if (!size) {
  461. ret = AVERROR(EAGAIN);
  462. goto leave;
  463. }
  464. ret= av_get_packet(s->pb, pkt, size);
  465. if (ret < 0) {
  466. return AVERROR(EIO);
  467. }
  468. /* note: we need to modify the packet size here to handle the last
  469. packet */
  470. pkt->size = ret;
  471. pkt->dts = dts;
  472. pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
  473. pkt->stream_index = st->index;
  474. if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
  475. pkt->flags |= AV_PKT_FLAG_KEY;
  476. leave:
  477. avio_skip(s->pb, 4);
  478. return ret;
  479. }
  480. static int flv_read_seek(AVFormatContext *s, int stream_index,
  481. int64_t ts, int flags)
  482. {
  483. return avio_seek_time(s->pb, stream_index, ts, flags);
  484. }
  485. #if 0 /* don't know enough to implement this */
  486. static int flv_read_seek2(AVFormatContext *s, int stream_index,
  487. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  488. {
  489. int ret = AVERROR(ENOSYS);
  490. if (ts - min_ts > (uint64_t)(max_ts - ts)) flags |= AVSEEK_FLAG_BACKWARD;
  491. if (!s->pb->seekable) {
  492. if (stream_index < 0) {
  493. stream_index = av_find_default_stream_index(s);
  494. if (stream_index < 0)
  495. return -1;
  496. /* timestamp for default must be expressed in AV_TIME_BASE units */
  497. ts = av_rescale_rnd(ts, 1000, AV_TIME_BASE,
  498. flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
  499. }
  500. ret = avio_seek_time(s->pb, stream_index, ts, flags);
  501. }
  502. if (ret == AVERROR(ENOSYS))
  503. ret = av_seek_frame(s, stream_index, ts, flags);
  504. return ret;
  505. }
  506. #endif
  507. AVInputFormat ff_flv_demuxer = {
  508. "flv",
  509. NULL_IF_CONFIG_SMALL("FLV format"),
  510. sizeof(FLVContext),
  511. flv_probe,
  512. flv_read_header,
  513. flv_read_packet,
  514. .read_seek = flv_read_seek,
  515. #if 0
  516. .read_seek2 = flv_read_seek2,
  517. #endif
  518. .extensions = "flv",
  519. .value = CODEC_ID_FLV1,
  520. };