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.

1286 lines
44KB

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