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.

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