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.

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