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.

1197 lines
41KB

  1. /*
  2. * FLV demuxer
  3. * Copyright (c) 2003 The FFmpeg 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 FFmpeg.
  11. *
  12. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/channel_layout.h"
  28. #include "libavutil/dict.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/intfloat.h"
  31. #include "libavutil/mathematics.h"
  32. #include "libavcodec/bytestream.h"
  33. #include "libavcodec/mpeg4audio.h"
  34. #include "avformat.h"
  35. #include "internal.h"
  36. #include "avio_internal.h"
  37. #include "flv.h"
  38. #define VALIDATE_INDEX_TS_THRESH 2500
  39. #define RESYNC_BUFFER_SIZE (1<<20)
  40. typedef struct FLVContext {
  41. const AVClass *class; ///< Class for private options.
  42. int trust_metadata; ///< configure streams according onMetaData
  43. int wrong_dts; ///< wrong dts due to negative cts
  44. uint8_t *new_extradata[FLV_STREAM_TYPE_NB];
  45. int new_extradata_size[FLV_STREAM_TYPE_NB];
  46. int last_sample_rate;
  47. int last_channels;
  48. struct {
  49. int64_t dts;
  50. int64_t pos;
  51. } validate_index[2];
  52. int validate_next;
  53. int validate_count;
  54. int searched_for_end;
  55. uint8_t resync_buffer[2*RESYNC_BUFFER_SIZE];
  56. } FLVContext;
  57. static int probe(AVProbeData *p, int live)
  58. {
  59. const uint8_t *d = p->buf;
  60. unsigned offset = AV_RB32(d + 5);
  61. if (d[0] == 'F' &&
  62. d[1] == 'L' &&
  63. d[2] == 'V' &&
  64. d[3] < 5 && d[5] == 0 &&
  65. offset + 100 < p->buf_size &&
  66. offset > 8) {
  67. int is_live = !memcmp(d + offset + 40, "NGINX RTMP", 10);
  68. if (live == is_live)
  69. return AVPROBE_SCORE_MAX;
  70. }
  71. return 0;
  72. }
  73. static int flv_probe(AVProbeData *p)
  74. {
  75. return probe(p, 0);
  76. }
  77. static int live_flv_probe(AVProbeData *p)
  78. {
  79. return probe(p, 1);
  80. }
  81. static AVStream *create_stream(AVFormatContext *s, int codec_type)
  82. {
  83. AVStream *st = avformat_new_stream(s, NULL);
  84. if (!st)
  85. return NULL;
  86. st->codec->codec_type = codec_type;
  87. if (s->nb_streams>=3 ||( s->nb_streams==2
  88. && s->streams[0]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE
  89. && s->streams[1]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE))
  90. s->ctx_flags &= ~AVFMTCTX_NOHEADER;
  91. avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
  92. return st;
  93. }
  94. static int flv_same_audio_codec(AVCodecContext *acodec, int flags)
  95. {
  96. int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
  97. int flv_codecid = flags & FLV_AUDIO_CODECID_MASK;
  98. int codec_id;
  99. if (!acodec->codec_id && !acodec->codec_tag)
  100. return 1;
  101. if (acodec->bits_per_coded_sample != bits_per_coded_sample)
  102. return 0;
  103. switch (flv_codecid) {
  104. // no distinction between S16 and S8 PCM codec flags
  105. case FLV_CODECID_PCM:
  106. codec_id = bits_per_coded_sample == 8
  107. ? AV_CODEC_ID_PCM_U8
  108. #if HAVE_BIGENDIAN
  109. : AV_CODEC_ID_PCM_S16BE;
  110. #else
  111. : AV_CODEC_ID_PCM_S16LE;
  112. #endif
  113. return codec_id == acodec->codec_id;
  114. case FLV_CODECID_PCM_LE:
  115. codec_id = bits_per_coded_sample == 8
  116. ? AV_CODEC_ID_PCM_U8
  117. : AV_CODEC_ID_PCM_S16LE;
  118. return codec_id == acodec->codec_id;
  119. case FLV_CODECID_AAC:
  120. return acodec->codec_id == AV_CODEC_ID_AAC;
  121. case FLV_CODECID_ADPCM:
  122. return acodec->codec_id == AV_CODEC_ID_ADPCM_SWF;
  123. case FLV_CODECID_SPEEX:
  124. return acodec->codec_id == AV_CODEC_ID_SPEEX;
  125. case FLV_CODECID_MP3:
  126. return acodec->codec_id == AV_CODEC_ID_MP3;
  127. case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
  128. case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
  129. case FLV_CODECID_NELLYMOSER:
  130. return acodec->codec_id == AV_CODEC_ID_NELLYMOSER;
  131. case FLV_CODECID_PCM_MULAW:
  132. return acodec->sample_rate == 8000 &&
  133. acodec->codec_id == AV_CODEC_ID_PCM_MULAW;
  134. case FLV_CODECID_PCM_ALAW:
  135. return acodec->sample_rate == 8000 &&
  136. acodec->codec_id == AV_CODEC_ID_PCM_ALAW;
  137. default:
  138. return acodec->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
  139. }
  140. }
  141. static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream,
  142. AVCodecContext *acodec, int flv_codecid)
  143. {
  144. switch (flv_codecid) {
  145. // no distinction between S16 and S8 PCM codec flags
  146. case FLV_CODECID_PCM:
  147. acodec->codec_id = acodec->bits_per_coded_sample == 8
  148. ? AV_CODEC_ID_PCM_U8
  149. #if HAVE_BIGENDIAN
  150. : AV_CODEC_ID_PCM_S16BE;
  151. #else
  152. : AV_CODEC_ID_PCM_S16LE;
  153. #endif
  154. break;
  155. case FLV_CODECID_PCM_LE:
  156. acodec->codec_id = acodec->bits_per_coded_sample == 8
  157. ? AV_CODEC_ID_PCM_U8
  158. : AV_CODEC_ID_PCM_S16LE;
  159. break;
  160. case FLV_CODECID_AAC:
  161. acodec->codec_id = AV_CODEC_ID_AAC;
  162. break;
  163. case FLV_CODECID_ADPCM:
  164. acodec->codec_id = AV_CODEC_ID_ADPCM_SWF;
  165. break;
  166. case FLV_CODECID_SPEEX:
  167. acodec->codec_id = AV_CODEC_ID_SPEEX;
  168. acodec->sample_rate = 16000;
  169. break;
  170. case FLV_CODECID_MP3:
  171. acodec->codec_id = AV_CODEC_ID_MP3;
  172. astream->need_parsing = AVSTREAM_PARSE_FULL;
  173. break;
  174. case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
  175. // in case metadata does not otherwise declare samplerate
  176. acodec->sample_rate = 8000;
  177. acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
  178. break;
  179. case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
  180. acodec->sample_rate = 16000;
  181. acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
  182. break;
  183. case FLV_CODECID_NELLYMOSER:
  184. acodec->codec_id = AV_CODEC_ID_NELLYMOSER;
  185. break;
  186. case FLV_CODECID_PCM_MULAW:
  187. acodec->sample_rate = 8000;
  188. acodec->codec_id = AV_CODEC_ID_PCM_MULAW;
  189. break;
  190. case FLV_CODECID_PCM_ALAW:
  191. acodec->sample_rate = 8000;
  192. acodec->codec_id = AV_CODEC_ID_PCM_ALAW;
  193. break;
  194. default:
  195. avpriv_request_sample(s, "Audio codec (%x)",
  196. flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
  197. acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
  198. }
  199. }
  200. static int flv_same_video_codec(AVCodecContext *vcodec, int flags)
  201. {
  202. int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;
  203. if (!vcodec->codec_id && !vcodec->codec_tag)
  204. return 1;
  205. switch (flv_codecid) {
  206. case FLV_CODECID_H263:
  207. return vcodec->codec_id == AV_CODEC_ID_FLV1;
  208. case FLV_CODECID_SCREEN:
  209. return vcodec->codec_id == AV_CODEC_ID_FLASHSV;
  210. case FLV_CODECID_SCREEN2:
  211. return vcodec->codec_id == AV_CODEC_ID_FLASHSV2;
  212. case FLV_CODECID_VP6:
  213. return vcodec->codec_id == AV_CODEC_ID_VP6F;
  214. case FLV_CODECID_VP6A:
  215. return vcodec->codec_id == AV_CODEC_ID_VP6A;
  216. case FLV_CODECID_H264:
  217. return vcodec->codec_id == AV_CODEC_ID_H264;
  218. default:
  219. return vcodec->codec_tag == flv_codecid;
  220. }
  221. }
  222. static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream,
  223. int flv_codecid, int read)
  224. {
  225. AVCodecContext *vcodec = vstream->codec;
  226. switch (flv_codecid) {
  227. case FLV_CODECID_H263:
  228. vcodec->codec_id = AV_CODEC_ID_FLV1;
  229. break;
  230. case FLV_CODECID_REALH263:
  231. vcodec->codec_id = AV_CODEC_ID_H263;
  232. break; // Really mean it this time
  233. case FLV_CODECID_SCREEN:
  234. vcodec->codec_id = AV_CODEC_ID_FLASHSV;
  235. break;
  236. case FLV_CODECID_SCREEN2:
  237. vcodec->codec_id = AV_CODEC_ID_FLASHSV2;
  238. break;
  239. case FLV_CODECID_VP6:
  240. vcodec->codec_id = AV_CODEC_ID_VP6F;
  241. case FLV_CODECID_VP6A:
  242. if (flv_codecid == FLV_CODECID_VP6A)
  243. vcodec->codec_id = AV_CODEC_ID_VP6A;
  244. if (read) {
  245. if (vcodec->extradata_size != 1) {
  246. ff_alloc_extradata(vcodec, 1);
  247. }
  248. if (vcodec->extradata)
  249. vcodec->extradata[0] = avio_r8(s->pb);
  250. else
  251. avio_skip(s->pb, 1);
  252. }
  253. return 1; // 1 byte body size adjustment for flv_read_packet()
  254. case FLV_CODECID_H264:
  255. vcodec->codec_id = AV_CODEC_ID_H264;
  256. vstream->need_parsing = AVSTREAM_PARSE_HEADERS;
  257. return 3; // not 4, reading packet type will consume one byte
  258. case FLV_CODECID_MPEG4:
  259. vcodec->codec_id = AV_CODEC_ID_MPEG4;
  260. return 3;
  261. default:
  262. avpriv_request_sample(s, "Video codec (%x)", flv_codecid);
  263. vcodec->codec_tag = flv_codecid;
  264. }
  265. return 0;
  266. }
  267. static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
  268. {
  269. int length = avio_rb16(ioc);
  270. if (length >= buffsize) {
  271. avio_skip(ioc, length);
  272. return -1;
  273. }
  274. avio_read(ioc, buffer, length);
  275. buffer[length] = '\0';
  276. return length;
  277. }
  278. static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc,
  279. AVStream *vstream, int64_t max_pos)
  280. {
  281. FLVContext *flv = s->priv_data;
  282. unsigned int timeslen = 0, fileposlen = 0, i;
  283. char str_val[256];
  284. int64_t *times = NULL;
  285. int64_t *filepositions = NULL;
  286. int ret = AVERROR(ENOSYS);
  287. int64_t initial_pos = avio_tell(ioc);
  288. if (vstream->nb_index_entries>0) {
  289. av_log(s, AV_LOG_WARNING, "Skipping duplicate index\n");
  290. return 0;
  291. }
  292. if (s->flags & AVFMT_FLAG_IGNIDX)
  293. return 0;
  294. while (avio_tell(ioc) < max_pos - 2 &&
  295. amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
  296. int64_t **current_array;
  297. unsigned int arraylen;
  298. // Expect array object in context
  299. if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
  300. break;
  301. arraylen = avio_rb32(ioc);
  302. if (arraylen>>28)
  303. break;
  304. if (!strcmp(KEYFRAMES_TIMESTAMP_TAG , str_val) && !times) {
  305. current_array = &times;
  306. timeslen = arraylen;
  307. } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) &&
  308. !filepositions) {
  309. current_array = &filepositions;
  310. fileposlen = arraylen;
  311. } else
  312. // unexpected metatag inside keyframes, will not use such
  313. // metadata for indexing
  314. break;
  315. if (!(*current_array = av_mallocz(sizeof(**current_array) * arraylen))) {
  316. ret = AVERROR(ENOMEM);
  317. goto finish;
  318. }
  319. for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
  320. if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
  321. goto invalid;
  322. current_array[0][i] = av_int2double(avio_rb64(ioc));
  323. }
  324. if (times && filepositions) {
  325. // All done, exiting at a position allowing amf_parse_object
  326. // to finish parsing the object
  327. ret = 0;
  328. break;
  329. }
  330. }
  331. if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
  332. for (i = 0; i < fileposlen; i++) {
  333. av_add_index_entry(vstream, filepositions[i], times[i] * 1000,
  334. 0, 0, AVINDEX_KEYFRAME);
  335. if (i < 2) {
  336. flv->validate_index[i].pos = filepositions[i];
  337. flv->validate_index[i].dts = times[i] * 1000;
  338. flv->validate_count = i + 1;
  339. }
  340. }
  341. } else {
  342. invalid:
  343. av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
  344. }
  345. finish:
  346. av_freep(&times);
  347. av_freep(&filepositions);
  348. avio_seek(ioc, initial_pos, SEEK_SET);
  349. return ret;
  350. }
  351. static int amf_parse_object(AVFormatContext *s, AVStream *astream,
  352. AVStream *vstream, const char *key,
  353. int64_t max_pos, int depth)
  354. {
  355. AVCodecContext *acodec, *vcodec;
  356. FLVContext *flv = s->priv_data;
  357. AVIOContext *ioc;
  358. AMFDataType amf_type;
  359. char str_val[1024];
  360. double num_val;
  361. num_val = 0;
  362. ioc = s->pb;
  363. amf_type = avio_r8(ioc);
  364. switch (amf_type) {
  365. case AMF_DATA_TYPE_NUMBER:
  366. num_val = av_int2double(avio_rb64(ioc));
  367. break;
  368. case AMF_DATA_TYPE_BOOL:
  369. num_val = avio_r8(ioc);
  370. break;
  371. case AMF_DATA_TYPE_STRING:
  372. if (amf_get_string(ioc, str_val, sizeof(str_val)) < 0) {
  373. av_log(s, AV_LOG_ERROR, "AMF_DATA_TYPE_STRING parsing failed\n");
  374. return -1;
  375. }
  376. break;
  377. case AMF_DATA_TYPE_OBJECT:
  378. if ((vstream || astream) && key &&
  379. ioc->seekable &&
  380. !strcmp(KEYFRAMES_TAG, key) && depth == 1)
  381. if (parse_keyframes_index(s, ioc, vstream ? vstream : astream,
  382. max_pos) < 0)
  383. av_log(s, AV_LOG_ERROR, "Keyframe index parsing failed\n");
  384. while (avio_tell(ioc) < max_pos - 2 &&
  385. amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
  386. if (amf_parse_object(s, astream, vstream, str_val, max_pos,
  387. depth + 1) < 0)
  388. return -1; // if we couldn't skip, bomb out.
  389. if (avio_r8(ioc) != AMF_END_OF_OBJECT) {
  390. av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_OBJECT\n");
  391. return -1;
  392. }
  393. break;
  394. case AMF_DATA_TYPE_NULL:
  395. case AMF_DATA_TYPE_UNDEFINED:
  396. case AMF_DATA_TYPE_UNSUPPORTED:
  397. break; // these take up no additional space
  398. case AMF_DATA_TYPE_MIXEDARRAY:
  399. {
  400. unsigned v;
  401. avio_skip(ioc, 4); // skip 32-bit max array index
  402. while (avio_tell(ioc) < max_pos - 2 &&
  403. amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
  404. // this is the only case in which we would want a nested
  405. // parse to not skip over the object
  406. if (amf_parse_object(s, astream, vstream, str_val, max_pos,
  407. depth + 1) < 0)
  408. return -1;
  409. v = avio_r8(ioc);
  410. if (v != AMF_END_OF_OBJECT) {
  411. av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_MIXEDARRAY, found %d\n", v);
  412. return -1;
  413. }
  414. break;
  415. }
  416. case AMF_DATA_TYPE_ARRAY:
  417. {
  418. unsigned int arraylen, i;
  419. arraylen = avio_rb32(ioc);
  420. for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++)
  421. if (amf_parse_object(s, NULL, NULL, NULL, max_pos,
  422. depth + 1) < 0)
  423. return -1; // if we couldn't skip, bomb out.
  424. }
  425. break;
  426. case AMF_DATA_TYPE_DATE:
  427. avio_skip(ioc, 8 + 2); // timestamp (double) and UTC offset (int16)
  428. break;
  429. default: // unsupported type, we couldn't skip
  430. av_log(s, AV_LOG_ERROR, "unsupported amf type %d\n", amf_type);
  431. return -1;
  432. }
  433. if (key) {
  434. acodec = astream ? astream->codec : NULL;
  435. vcodec = vstream ? vstream->codec : NULL;
  436. // stream info doesn't live any deeper than the first object
  437. if (depth == 1) {
  438. if (amf_type == AMF_DATA_TYPE_NUMBER ||
  439. amf_type == AMF_DATA_TYPE_BOOL) {
  440. if (!strcmp(key, "duration"))
  441. s->duration = num_val * AV_TIME_BASE;
  442. else if (!strcmp(key, "videodatarate") && vcodec &&
  443. 0 <= (int)(num_val * 1024.0))
  444. vcodec->bit_rate = num_val * 1024.0;
  445. else if (!strcmp(key, "audiodatarate") && acodec &&
  446. 0 <= (int)(num_val * 1024.0))
  447. acodec->bit_rate = num_val * 1024.0;
  448. else if (!strcmp(key, "datastream")) {
  449. AVStream *st = create_stream(s, AVMEDIA_TYPE_SUBTITLE);
  450. if (!st)
  451. return AVERROR(ENOMEM);
  452. st->codec->codec_id = AV_CODEC_ID_TEXT;
  453. } else if (flv->trust_metadata) {
  454. if (!strcmp(key, "videocodecid") && vcodec) {
  455. flv_set_video_codec(s, vstream, num_val, 0);
  456. } else if (!strcmp(key, "audiocodecid") && acodec) {
  457. int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
  458. flv_set_audio_codec(s, astream, acodec, id);
  459. } else if (!strcmp(key, "audiosamplerate") && acodec) {
  460. acodec->sample_rate = num_val;
  461. } else if (!strcmp(key, "audiosamplesize") && acodec) {
  462. acodec->bits_per_coded_sample = num_val;
  463. } else if (!strcmp(key, "stereo") && acodec) {
  464. acodec->channels = num_val + 1;
  465. acodec->channel_layout = acodec->channels == 2 ?
  466. AV_CH_LAYOUT_STEREO :
  467. AV_CH_LAYOUT_MONO;
  468. } else if (!strcmp(key, "width") && vcodec) {
  469. vcodec->width = num_val;
  470. } else if (!strcmp(key, "height") && vcodec) {
  471. vcodec->height = num_val;
  472. }
  473. }
  474. }
  475. }
  476. if (amf_type == AMF_DATA_TYPE_OBJECT && s->nb_streams == 1 &&
  477. ((!acodec && !strcmp(key, "audiocodecid")) ||
  478. (!vcodec && !strcmp(key, "videocodecid"))))
  479. s->ctx_flags &= ~AVFMTCTX_NOHEADER; //If there is either audio/video missing, codecid will be an empty object
  480. if (!strcmp(key, "duration") ||
  481. !strcmp(key, "filesize") ||
  482. !strcmp(key, "width") ||
  483. !strcmp(key, "height") ||
  484. !strcmp(key, "videodatarate") ||
  485. !strcmp(key, "framerate") ||
  486. !strcmp(key, "videocodecid") ||
  487. !strcmp(key, "audiodatarate") ||
  488. !strcmp(key, "audiosamplerate") ||
  489. !strcmp(key, "audiosamplesize") ||
  490. !strcmp(key, "stereo") ||
  491. !strcmp(key, "audiocodecid") ||
  492. !strcmp(key, "datastream"))
  493. return 0;
  494. s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  495. if (amf_type == AMF_DATA_TYPE_BOOL) {
  496. av_strlcpy(str_val, num_val > 0 ? "true" : "false",
  497. sizeof(str_val));
  498. av_dict_set(&s->metadata, key, str_val, 0);
  499. } else if (amf_type == AMF_DATA_TYPE_NUMBER) {
  500. snprintf(str_val, sizeof(str_val), "%.f", num_val);
  501. av_dict_set(&s->metadata, key, str_val, 0);
  502. } else if (amf_type == AMF_DATA_TYPE_STRING)
  503. av_dict_set(&s->metadata, key, str_val, 0);
  504. }
  505. return 0;
  506. }
  507. #define TYPE_ONTEXTDATA 1
  508. #define TYPE_ONCAPTION 2
  509. #define TYPE_ONCAPTIONINFO 3
  510. #define TYPE_UNKNOWN 9
  511. static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
  512. {
  513. AMFDataType type;
  514. AVStream *stream, *astream, *vstream;
  515. AVStream av_unused *dstream;
  516. AVIOContext *ioc;
  517. int i;
  518. // only needs to hold the string "onMetaData".
  519. // Anything longer is something we don't want.
  520. char buffer[32];
  521. astream = NULL;
  522. vstream = NULL;
  523. dstream = NULL;
  524. ioc = s->pb;
  525. // first object needs to be "onMetaData" string
  526. type = avio_r8(ioc);
  527. if (type != AMF_DATA_TYPE_STRING ||
  528. amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
  529. return TYPE_UNKNOWN;
  530. if (!strcmp(buffer, "onTextData"))
  531. return TYPE_ONTEXTDATA;
  532. if (!strcmp(buffer, "onCaption"))
  533. return TYPE_ONCAPTION;
  534. if (!strcmp(buffer, "onCaptionInfo"))
  535. return TYPE_ONCAPTIONINFO;
  536. if (strcmp(buffer, "onMetaData") && strcmp(buffer, "onCuePoint")) {
  537. av_log(s, AV_LOG_DEBUG, "Unknown type %s\n", buffer);
  538. return TYPE_UNKNOWN;
  539. }
  540. // find the streams now so that amf_parse_object doesn't need to do
  541. // the lookup every time it is called.
  542. for (i = 0; i < s->nb_streams; i++) {
  543. stream = s->streams[i];
  544. if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  545. vstream = stream;
  546. else if (stream->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  547. astream = stream;
  548. else if (stream->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
  549. dstream = stream;
  550. }
  551. // parse the second object (we want a mixed array)
  552. if (amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
  553. return -1;
  554. return 0;
  555. }
  556. static int flv_read_header(AVFormatContext *s)
  557. {
  558. int offset, flags;
  559. avio_skip(s->pb, 4);
  560. flags = avio_r8(s->pb);
  561. s->ctx_flags |= AVFMTCTX_NOHEADER;
  562. if (flags & FLV_HEADER_FLAG_HASVIDEO)
  563. if (!create_stream(s, AVMEDIA_TYPE_VIDEO))
  564. return AVERROR(ENOMEM);
  565. if (flags & FLV_HEADER_FLAG_HASAUDIO)
  566. if (!create_stream(s, AVMEDIA_TYPE_AUDIO))
  567. return AVERROR(ENOMEM);
  568. // Flag doesn't indicate whether or not there is script-data present. Must
  569. // create that stream if it's encountered.
  570. offset = avio_rb32(s->pb);
  571. avio_seek(s->pb, offset, SEEK_SET);
  572. avio_skip(s->pb, 4);
  573. s->start_time = 0;
  574. return 0;
  575. }
  576. static int flv_read_close(AVFormatContext *s)
  577. {
  578. int i;
  579. FLVContext *flv = s->priv_data;
  580. for (i=0; i<FLV_STREAM_TYPE_NB; i++)
  581. av_freep(&flv->new_extradata[i]);
  582. return 0;
  583. }
  584. static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
  585. {
  586. av_freep(&st->codec->extradata);
  587. if (ff_get_extradata(st->codec, s->pb, size) < 0)
  588. return AVERROR(ENOMEM);
  589. return 0;
  590. }
  591. static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
  592. int size)
  593. {
  594. av_free(flv->new_extradata[stream]);
  595. flv->new_extradata[stream] = av_mallocz(size +
  596. AV_INPUT_BUFFER_PADDING_SIZE);
  597. if (!flv->new_extradata[stream])
  598. return AVERROR(ENOMEM);
  599. flv->new_extradata_size[stream] = size;
  600. avio_read(pb, flv->new_extradata[stream], size);
  601. return 0;
  602. }
  603. static void clear_index_entries(AVFormatContext *s, int64_t pos)
  604. {
  605. int i, j, out;
  606. av_log(s, AV_LOG_WARNING,
  607. "Found invalid index entries, clearing the index.\n");
  608. for (i = 0; i < s->nb_streams; i++) {
  609. AVStream *st = s->streams[i];
  610. /* Remove all index entries that point to >= pos */
  611. out = 0;
  612. for (j = 0; j < st->nb_index_entries; j++)
  613. if (st->index_entries[j].pos < pos)
  614. st->index_entries[out++] = st->index_entries[j];
  615. st->nb_index_entries = out;
  616. }
  617. }
  618. static int amf_skip_tag(AVIOContext *pb, AMFDataType type)
  619. {
  620. int nb = -1, ret, parse_name = 1;
  621. switch (type) {
  622. case AMF_DATA_TYPE_NUMBER:
  623. avio_skip(pb, 8);
  624. break;
  625. case AMF_DATA_TYPE_BOOL:
  626. avio_skip(pb, 1);
  627. break;
  628. case AMF_DATA_TYPE_STRING:
  629. avio_skip(pb, avio_rb16(pb));
  630. break;
  631. case AMF_DATA_TYPE_ARRAY:
  632. parse_name = 0;
  633. case AMF_DATA_TYPE_MIXEDARRAY:
  634. nb = avio_rb32(pb);
  635. case AMF_DATA_TYPE_OBJECT:
  636. while(!pb->eof_reached && (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY)) {
  637. if (parse_name) {
  638. int size = avio_rb16(pb);
  639. if (!size) {
  640. avio_skip(pb, 1);
  641. break;
  642. }
  643. avio_skip(pb, size);
  644. }
  645. if ((ret = amf_skip_tag(pb, avio_r8(pb))) < 0)
  646. return ret;
  647. }
  648. break;
  649. case AMF_DATA_TYPE_NULL:
  650. case AMF_DATA_TYPE_OBJECT_END:
  651. break;
  652. default:
  653. return AVERROR_INVALIDDATA;
  654. }
  655. return 0;
  656. }
  657. static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
  658. int64_t dts, int64_t next)
  659. {
  660. AVIOContext *pb = s->pb;
  661. AVStream *st = NULL;
  662. char buf[20];
  663. int ret = AVERROR_INVALIDDATA;
  664. int i, length = -1;
  665. int array = 0;
  666. switch (avio_r8(pb)) {
  667. case AMF_DATA_TYPE_ARRAY:
  668. array = 1;
  669. case AMF_DATA_TYPE_MIXEDARRAY:
  670. avio_seek(pb, 4, SEEK_CUR);
  671. case AMF_DATA_TYPE_OBJECT:
  672. break;
  673. default:
  674. goto skip;
  675. }
  676. while (array || (ret = amf_get_string(pb, buf, sizeof(buf))) > 0) {
  677. AMFDataType type = avio_r8(pb);
  678. if (type == AMF_DATA_TYPE_STRING && (array || !strcmp(buf, "text"))) {
  679. length = avio_rb16(pb);
  680. ret = av_get_packet(pb, pkt, length);
  681. if (ret < 0)
  682. goto skip;
  683. else
  684. break;
  685. } else {
  686. if ((ret = amf_skip_tag(pb, type)) < 0)
  687. goto skip;
  688. }
  689. }
  690. if (length < 0) {
  691. ret = AVERROR_INVALIDDATA;
  692. goto skip;
  693. }
  694. for (i = 0; i < s->nb_streams; i++) {
  695. st = s->streams[i];
  696. if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
  697. break;
  698. }
  699. if (i == s->nb_streams) {
  700. st = create_stream(s, AVMEDIA_TYPE_SUBTITLE);
  701. if (!st)
  702. return AVERROR(ENOMEM);
  703. st->codec->codec_id = AV_CODEC_ID_TEXT;
  704. }
  705. pkt->dts = dts;
  706. pkt->pts = dts;
  707. pkt->size = ret;
  708. pkt->stream_index = st->index;
  709. pkt->flags |= AV_PKT_FLAG_KEY;
  710. skip:
  711. avio_seek(s->pb, next + 4, SEEK_SET);
  712. return ret;
  713. }
  714. static int resync(AVFormatContext *s)
  715. {
  716. FLVContext *flv = s->priv_data;
  717. int64_t i;
  718. int64_t pos = avio_tell(s->pb);
  719. for (i=0; !avio_feof(s->pb); i++) {
  720. int j = i & (RESYNC_BUFFER_SIZE-1);
  721. int j1 = j + RESYNC_BUFFER_SIZE;
  722. flv->resync_buffer[j ] =
  723. flv->resync_buffer[j1] = avio_r8(s->pb);
  724. if (i > 22) {
  725. unsigned lsize2 = AV_RB32(flv->resync_buffer + j1 - 4);
  726. if (lsize2 >= 11 && lsize2 + 8LL < FFMIN(i, RESYNC_BUFFER_SIZE)) {
  727. unsigned size2 = AV_RB24(flv->resync_buffer + j1 - lsize2 + 1 - 4);
  728. unsigned lsize1 = AV_RB32(flv->resync_buffer + j1 - lsize2 - 8);
  729. if (lsize1 >= 11 && lsize1 + 8LL + lsize2 < FFMIN(i, RESYNC_BUFFER_SIZE)) {
  730. unsigned size1 = AV_RB24(flv->resync_buffer + j1 - lsize1 + 1 - lsize2 - 8);
  731. if (size1 == lsize1 - 11 && size2 == lsize2 - 11) {
  732. avio_seek(s->pb, pos + i - lsize1 - lsize2 - 8, SEEK_SET);
  733. return 1;
  734. }
  735. }
  736. }
  737. }
  738. }
  739. return AVERROR_EOF;
  740. }
  741. static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
  742. {
  743. FLVContext *flv = s->priv_data;
  744. int ret, i, size, flags;
  745. enum FlvTagType type;
  746. int stream_type=-1;
  747. int64_t next, pos, meta_pos;
  748. int64_t dts, pts = AV_NOPTS_VALUE;
  749. int av_uninit(channels);
  750. int av_uninit(sample_rate);
  751. AVStream *st = NULL;
  752. int last = -1;
  753. int orig_size;
  754. retry:
  755. /* pkt size is repeated at end. skip it */
  756. for (;; last = avio_rb32(s->pb)) {
  757. pos = avio_tell(s->pb);
  758. type = (avio_r8(s->pb) & 0x1F);
  759. orig_size =
  760. size = avio_rb24(s->pb);
  761. dts = avio_rb24(s->pb);
  762. dts |= avio_r8(s->pb) << 24;
  763. av_log(s, AV_LOG_TRACE, "type:%d, size:%d, last:%d, dts:%"PRId64" pos:%"PRId64"\n", type, size, last, dts, avio_tell(s->pb));
  764. if (avio_feof(s->pb))
  765. return AVERROR_EOF;
  766. avio_skip(s->pb, 3); /* stream id, always 0 */
  767. flags = 0;
  768. if (flv->validate_next < flv->validate_count) {
  769. int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
  770. if (pos == validate_pos) {
  771. if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
  772. VALIDATE_INDEX_TS_THRESH) {
  773. flv->validate_next++;
  774. } else {
  775. clear_index_entries(s, validate_pos);
  776. flv->validate_count = 0;
  777. }
  778. } else if (pos > validate_pos) {
  779. clear_index_entries(s, validate_pos);
  780. flv->validate_count = 0;
  781. }
  782. }
  783. if (size == 0) {
  784. ret = AVERROR(EAGAIN);
  785. goto leave;
  786. }
  787. next = size + avio_tell(s->pb);
  788. if (type == FLV_TAG_TYPE_AUDIO) {
  789. stream_type = FLV_STREAM_TYPE_AUDIO;
  790. flags = avio_r8(s->pb);
  791. size--;
  792. } else if (type == FLV_TAG_TYPE_VIDEO) {
  793. stream_type = FLV_STREAM_TYPE_VIDEO;
  794. flags = avio_r8(s->pb);
  795. size--;
  796. if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_VIDEO_INFO_CMD)
  797. goto skip;
  798. } else if (type == FLV_TAG_TYPE_META) {
  799. stream_type=FLV_STREAM_TYPE_DATA;
  800. if (size > 13 + 1 + 4) { // Header-type metadata stuff
  801. int type;
  802. meta_pos = avio_tell(s->pb);
  803. type = flv_read_metabody(s, next);
  804. if (type == 0 && dts == 0 || type < 0 || type == TYPE_UNKNOWN) {
  805. if (type < 0 && flv->validate_count &&
  806. flv->validate_index[0].pos > next &&
  807. flv->validate_index[0].pos - 4 < next
  808. ) {
  809. av_log(s, AV_LOG_WARNING, "Adjusting next position due to index mismatch\n");
  810. next = flv->validate_index[0].pos - 4;
  811. }
  812. goto skip;
  813. } else if (type == TYPE_ONTEXTDATA) {
  814. avpriv_request_sample(s, "OnTextData packet");
  815. return flv_data_packet(s, pkt, dts, next);
  816. } else if (type == TYPE_ONCAPTION) {
  817. return flv_data_packet(s, pkt, dts, next);
  818. }
  819. avio_seek(s->pb, meta_pos, SEEK_SET);
  820. }
  821. } else {
  822. av_log(s, AV_LOG_DEBUG,
  823. "Skipping flv packet: type %d, size %d, flags %d.\n",
  824. type, size, flags);
  825. skip:
  826. avio_seek(s->pb, next, SEEK_SET);
  827. ret = AVERROR(EAGAIN);
  828. goto leave;
  829. }
  830. /* skip empty data packets */
  831. if (!size) {
  832. ret = AVERROR(EAGAIN);
  833. goto leave;
  834. }
  835. /* now find stream */
  836. for (i = 0; i < s->nb_streams; i++) {
  837. st = s->streams[i];
  838. if (stream_type == FLV_STREAM_TYPE_AUDIO) {
  839. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  840. (s->audio_codec_id || flv_same_audio_codec(st->codec, flags)))
  841. break;
  842. } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
  843. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  844. (s->video_codec_id || flv_same_video_codec(st->codec, flags)))
  845. break;
  846. } else if (stream_type == FLV_STREAM_TYPE_DATA) {
  847. if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
  848. break;
  849. }
  850. }
  851. if (i == s->nb_streams) {
  852. static const enum AVMediaType stream_types[] = {AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_SUBTITLE};
  853. av_log(s, AV_LOG_WARNING, "Stream discovered after head already parsed\n");
  854. st = create_stream(s, stream_types[stream_type]);
  855. if (!st)
  856. return AVERROR(ENOMEM);
  857. }
  858. av_log(s, AV_LOG_TRACE, "%d %X %d \n", stream_type, flags, st->discard);
  859. if (s->pb->seekable &&
  860. ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||
  861. stream_type == FLV_STREAM_TYPE_AUDIO))
  862. av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
  863. if ( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || (stream_type == FLV_STREAM_TYPE_AUDIO)))
  864. ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && (stream_type == FLV_STREAM_TYPE_VIDEO)))
  865. || st->discard >= AVDISCARD_ALL
  866. ) {
  867. avio_seek(s->pb, next, SEEK_SET);
  868. ret = AVERROR(EAGAIN);
  869. goto leave;
  870. }
  871. break;
  872. }
  873. // if not streamed and no duration from metadata then seek to end to find
  874. // the duration from the timestamps
  875. if (s->pb->seekable && (!s->duration || s->duration == AV_NOPTS_VALUE) && !flv->searched_for_end) {
  876. int size;
  877. const int64_t pos = avio_tell(s->pb);
  878. // Read the last 4 bytes of the file, this should be the size of the
  879. // previous FLV tag. Use the timestamp of its payload as duration.
  880. int64_t fsize = avio_size(s->pb);
  881. retry_duration:
  882. avio_seek(s->pb, fsize - 4, SEEK_SET);
  883. size = avio_rb32(s->pb);
  884. // Seek to the start of the last FLV tag at position (fsize - 4 - size)
  885. // but skip the byte indicating the type.
  886. avio_seek(s->pb, fsize - 3 - size, SEEK_SET);
  887. if (size == avio_rb24(s->pb) + 11) {
  888. uint32_t ts = avio_rb24(s->pb);
  889. ts |= avio_r8(s->pb) << 24;
  890. if (ts)
  891. s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
  892. else if (fsize >= 8 && fsize - 8 >= size) {
  893. fsize -= size+4;
  894. goto retry_duration;
  895. }
  896. }
  897. avio_seek(s->pb, pos, SEEK_SET);
  898. flv->searched_for_end = 1;
  899. }
  900. if (stream_type == FLV_STREAM_TYPE_AUDIO) {
  901. int bits_per_coded_sample;
  902. channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
  903. sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>
  904. FLV_AUDIO_SAMPLERATE_OFFSET) >> 3;
  905. bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
  906. if (!st->codec->channels || !st->codec->sample_rate ||
  907. !st->codec->bits_per_coded_sample) {
  908. st->codec->channels = channels;
  909. st->codec->channel_layout = channels == 1
  910. ? AV_CH_LAYOUT_MONO
  911. : AV_CH_LAYOUT_STEREO;
  912. st->codec->sample_rate = sample_rate;
  913. st->codec->bits_per_coded_sample = bits_per_coded_sample;
  914. }
  915. if (!st->codec->codec_id) {
  916. flv_set_audio_codec(s, st, st->codec,
  917. flags & FLV_AUDIO_CODECID_MASK);
  918. flv->last_sample_rate =
  919. sample_rate = st->codec->sample_rate;
  920. flv->last_channels =
  921. channels = st->codec->channels;
  922. } else {
  923. AVCodecContext ctx = {0};
  924. ctx.sample_rate = sample_rate;
  925. ctx.bits_per_coded_sample = bits_per_coded_sample;
  926. flv_set_audio_codec(s, st, &ctx, flags & FLV_AUDIO_CODECID_MASK);
  927. sample_rate = ctx.sample_rate;
  928. }
  929. } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
  930. size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
  931. } else if (stream_type == FLV_STREAM_TYPE_DATA) {
  932. st->codec->codec_id = AV_CODEC_ID_TEXT;
  933. }
  934. if (st->codec->codec_id == AV_CODEC_ID_AAC ||
  935. st->codec->codec_id == AV_CODEC_ID_H264 ||
  936. st->codec->codec_id == AV_CODEC_ID_MPEG4) {
  937. int type = avio_r8(s->pb);
  938. size--;
  939. if (st->codec->codec_id == AV_CODEC_ID_H264 || st->codec->codec_id == AV_CODEC_ID_MPEG4) {
  940. // sign extension
  941. int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
  942. pts = dts + cts;
  943. if (cts < 0) { // dts might be wrong
  944. if (!flv->wrong_dts)
  945. av_log(s, AV_LOG_WARNING,
  946. "Negative cts, previous timestamps might be wrong.\n");
  947. flv->wrong_dts = 1;
  948. } else if (FFABS(dts - pts) > 1000*60*15) {
  949. av_log(s, AV_LOG_WARNING,
  950. "invalid timestamps %"PRId64" %"PRId64"\n", dts, pts);
  951. dts = pts = AV_NOPTS_VALUE;
  952. }
  953. }
  954. if (type == 0 && (!st->codec->extradata || st->codec->codec_id == AV_CODEC_ID_AAC ||
  955. st->codec->codec_id == AV_CODEC_ID_H264)) {
  956. AVDictionaryEntry *t;
  957. if (st->codec->extradata) {
  958. if ((ret = flv_queue_extradata(flv, s->pb, stream_type, size)) < 0)
  959. return ret;
  960. ret = AVERROR(EAGAIN);
  961. goto leave;
  962. }
  963. if ((ret = flv_get_extradata(s, st, size)) < 0)
  964. return ret;
  965. /* Workaround for buggy Omnia A/XE encoder */
  966. t = av_dict_get(s->metadata, "Encoder", NULL, 0);
  967. if (st->codec->codec_id == AV_CODEC_ID_AAC && t && !strcmp(t->value, "Omnia A/XE"))
  968. st->codec->extradata_size = 2;
  969. if (st->codec->codec_id == AV_CODEC_ID_AAC && 0) {
  970. MPEG4AudioConfig cfg;
  971. if (avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
  972. st->codec->extradata_size * 8, 1) >= 0) {
  973. st->codec->channels = cfg.channels;
  974. st->codec->channel_layout = 0;
  975. if (cfg.ext_sample_rate)
  976. st->codec->sample_rate = cfg.ext_sample_rate;
  977. else
  978. st->codec->sample_rate = cfg.sample_rate;
  979. av_log(s, AV_LOG_TRACE, "mp4a config channels %d sample rate %d\n",
  980. st->codec->channels, st->codec->sample_rate);
  981. }
  982. }
  983. ret = AVERROR(EAGAIN);
  984. goto leave;
  985. }
  986. }
  987. /* skip empty data packets */
  988. if (!size) {
  989. ret = AVERROR(EAGAIN);
  990. goto leave;
  991. }
  992. ret = av_get_packet(s->pb, pkt, size);
  993. if (ret < 0)
  994. return ret;
  995. pkt->dts = dts;
  996. pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
  997. pkt->stream_index = st->index;
  998. if (flv->new_extradata[stream_type]) {
  999. uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
  1000. flv->new_extradata_size[stream_type]);
  1001. if (side) {
  1002. memcpy(side, flv->new_extradata[stream_type],
  1003. flv->new_extradata_size[stream_type]);
  1004. av_freep(&flv->new_extradata[stream_type]);
  1005. flv->new_extradata_size[stream_type] = 0;
  1006. }
  1007. }
  1008. if (stream_type == FLV_STREAM_TYPE_AUDIO &&
  1009. (sample_rate != flv->last_sample_rate ||
  1010. channels != flv->last_channels)) {
  1011. flv->last_sample_rate = sample_rate;
  1012. flv->last_channels = channels;
  1013. ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
  1014. }
  1015. if ( stream_type == FLV_STREAM_TYPE_AUDIO ||
  1016. ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY) ||
  1017. stream_type == FLV_STREAM_TYPE_DATA)
  1018. pkt->flags |= AV_PKT_FLAG_KEY;
  1019. leave:
  1020. last = avio_rb32(s->pb);
  1021. if (last != orig_size + 11) {
  1022. av_log(s, AV_LOG_ERROR, "Packet mismatch %d %d\n", last, orig_size + 11);
  1023. avio_seek(s->pb, pos + 1, SEEK_SET);
  1024. ret = resync(s);
  1025. if (ret >= 0) {
  1026. av_free_packet(pkt);
  1027. goto retry;
  1028. }
  1029. }
  1030. return ret;
  1031. }
  1032. static int flv_read_seek(AVFormatContext *s, int stream_index,
  1033. int64_t ts, int flags)
  1034. {
  1035. FLVContext *flv = s->priv_data;
  1036. flv->validate_count = 0;
  1037. return avio_seek_time(s->pb, stream_index, ts, flags);
  1038. }
  1039. #define OFFSET(x) offsetof(FLVContext, x)
  1040. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1041. static const AVOption options[] = {
  1042. { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
  1043. { NULL }
  1044. };
  1045. static const AVClass flv_class = {
  1046. .class_name = "flvdec",
  1047. .item_name = av_default_item_name,
  1048. .option = options,
  1049. .version = LIBAVUTIL_VERSION_INT,
  1050. };
  1051. AVInputFormat ff_flv_demuxer = {
  1052. .name = "flv",
  1053. .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
  1054. .priv_data_size = sizeof(FLVContext),
  1055. .read_probe = flv_probe,
  1056. .read_header = flv_read_header,
  1057. .read_packet = flv_read_packet,
  1058. .read_seek = flv_read_seek,
  1059. .read_close = flv_read_close,
  1060. .extensions = "flv",
  1061. .priv_class = &flv_class,
  1062. };
  1063. static const AVClass live_flv_class = {
  1064. .class_name = "live_flvdec",
  1065. .item_name = av_default_item_name,
  1066. .option = options,
  1067. .version = LIBAVUTIL_VERSION_INT,
  1068. };
  1069. AVInputFormat ff_live_flv_demuxer = {
  1070. .name = "live_flv",
  1071. .long_name = NULL_IF_CONFIG_SMALL("live RTMP FLV (Flash Video)"),
  1072. .priv_data_size = sizeof(FLVContext),
  1073. .read_probe = live_flv_probe,
  1074. .read_header = flv_read_header,
  1075. .read_packet = flv_read_packet,
  1076. .read_seek = flv_read_seek,
  1077. .read_close = flv_read_close,
  1078. .extensions = "flv",
  1079. .priv_class = &live_flv_class,
  1080. .flags = AVFMT_TS_DISCONT
  1081. };