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.

1209 lines
42KB

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