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.

1312 lines
45KB

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