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.

459 lines
16KB

  1. /*
  2. * FLV demuxer
  3. * Copyright (c) 2003 The FFmpeg Project
  4. *
  5. * This demuxer will generate a 1 byte extradata for VP6F content.
  6. * It is composed of:
  7. * - upper 4bits: difference between encoded width and visible width
  8. * - lower 4bits: difference between encoded height and visible height
  9. *
  10. * This file is part of FFmpeg.
  11. *
  12. * FFmpeg is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * FFmpeg is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with FFmpeg; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "libavcodec/bytestream.h"
  28. #include "libavcodec/mpeg4audio.h"
  29. #include "avformat.h"
  30. #include "flv.h"
  31. typedef struct {
  32. int wrong_dts; ///< wrong dts due to negative cts
  33. } FLVContext;
  34. static int flv_probe(AVProbeData *p)
  35. {
  36. const uint8_t *d;
  37. d = p->buf;
  38. if (d[0] == 'F' && d[1] == 'L' && d[2] == 'V' && d[3] < 5 && d[5]==0 && AV_RB32(d+5)>8) {
  39. return AVPROBE_SCORE_MAX;
  40. }
  41. return 0;
  42. }
  43. static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, int flv_codecid) {
  44. AVCodecContext *acodec = astream->codec;
  45. switch(flv_codecid) {
  46. //no distinction between S16 and S8 PCM codec flags
  47. case FLV_CODECID_PCM:
  48. acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_S8 :
  49. #if HAVE_BIGENDIAN
  50. CODEC_ID_PCM_S16BE;
  51. #else
  52. CODEC_ID_PCM_S16LE;
  53. #endif
  54. break;
  55. case FLV_CODECID_PCM_LE:
  56. acodec->codec_id = acodec->bits_per_coded_sample == 8 ? CODEC_ID_PCM_S8 : CODEC_ID_PCM_S16LE; break;
  57. case FLV_CODECID_AAC : acodec->codec_id = CODEC_ID_AAC; break;
  58. case FLV_CODECID_ADPCM: acodec->codec_id = CODEC_ID_ADPCM_SWF; break;
  59. case FLV_CODECID_SPEEX:
  60. acodec->codec_id = CODEC_ID_SPEEX;
  61. acodec->sample_rate = 16000;
  62. break;
  63. case FLV_CODECID_MP3 : acodec->codec_id = CODEC_ID_MP3 ; astream->need_parsing = AVSTREAM_PARSE_FULL; break;
  64. case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
  65. acodec->sample_rate = 8000; //in case metadata does not otherwise declare samplerate
  66. case FLV_CODECID_NELLYMOSER:
  67. acodec->codec_id = CODEC_ID_NELLYMOSER;
  68. break;
  69. default:
  70. av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
  71. acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
  72. }
  73. }
  74. static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
  75. AVCodecContext *vcodec = vstream->codec;
  76. switch(flv_codecid) {
  77. case FLV_CODECID_H263 : vcodec->codec_id = CODEC_ID_FLV1 ; break;
  78. case FLV_CODECID_SCREEN: vcodec->codec_id = CODEC_ID_FLASHSV; break;
  79. case FLV_CODECID_SCREEN2: vcodec->codec_id = CODEC_ID_FLASHSV2; break;
  80. case FLV_CODECID_VP6 : vcodec->codec_id = CODEC_ID_VP6F ;
  81. case FLV_CODECID_VP6A :
  82. if(flv_codecid == FLV_CODECID_VP6A)
  83. vcodec->codec_id = CODEC_ID_VP6A;
  84. if(vcodec->extradata_size != 1) {
  85. vcodec->extradata_size = 1;
  86. vcodec->extradata = av_malloc(1);
  87. }
  88. vcodec->extradata[0] = get_byte(s->pb);
  89. return 1; // 1 byte body size adjustment for flv_read_packet()
  90. case FLV_CODECID_H264:
  91. vcodec->codec_id = CODEC_ID_H264;
  92. return 3; // not 4, reading packet type will consume one byte
  93. default:
  94. av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid);
  95. vcodec->codec_tag = flv_codecid;
  96. }
  97. return 0;
  98. }
  99. static int amf_get_string(ByteIOContext *ioc, char **buf)
  100. {
  101. uint16_t len = get_be16(ioc);
  102. *buf = av_malloc(len+1);
  103. if (!*buf)
  104. return AVERROR_NOMEM;
  105. get_buffer(ioc, *buf, len);
  106. (*buf)[len] = '\0';
  107. return len;
  108. }
  109. static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, int64_t max_pos, int depth) {
  110. AVCodecContext *acodec, *vcodec;
  111. ByteIOContext *ioc;
  112. AMFDataType amf_type;
  113. char *str = NULL;
  114. double num_val;
  115. num_val = 0;
  116. ioc = s->pb;
  117. amf_type = get_byte(ioc);
  118. switch(amf_type) {
  119. case AMF_DATA_TYPE_NUMBER:
  120. num_val = av_int2dbl(get_be64(ioc)); break;
  121. case AMF_DATA_TYPE_BOOL:
  122. num_val = get_byte(ioc); break;
  123. case AMF_DATA_TYPE_STRING:
  124. if(amf_get_string(ioc, &str) < 0)
  125. return -1;
  126. break;
  127. case AMF_DATA_TYPE_OBJECT: {
  128. unsigned int keylen;
  129. while(url_ftell(ioc) < max_pos - 2 && (keylen = get_be16(ioc))) {
  130. url_fskip(ioc, keylen); //skip key string
  131. if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
  132. return -1; //if we couldn't skip, bomb out.
  133. }
  134. if(get_byte(ioc) != AMF_END_OF_OBJECT)
  135. return -1;
  136. }
  137. break;
  138. case AMF_DATA_TYPE_NULL:
  139. case AMF_DATA_TYPE_UNDEFINED:
  140. case AMF_DATA_TYPE_UNSUPPORTED:
  141. break; //these take up no additional space
  142. case AMF_DATA_TYPE_MIXEDARRAY:
  143. url_fskip(ioc, 4); //skip 32-bit max array index
  144. while(url_ftell(ioc) < max_pos - 2 && amf_get_string(ioc, &str) > 0) {
  145. //this is the only case in which we would want a nested parse to not skip over the object
  146. if(amf_parse_object(s, astream, vstream, str, max_pos, depth + 1) < 0)
  147. return -1;
  148. av_freep(&str);
  149. }
  150. if(get_byte(ioc) != AMF_END_OF_OBJECT)
  151. return -1;
  152. break;
  153. case AMF_DATA_TYPE_ARRAY: {
  154. unsigned int arraylen, i;
  155. arraylen = get_be32(ioc);
  156. for(i = 0; i < arraylen && url_ftell(ioc) < max_pos - 1; i++) {
  157. if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
  158. return -1; //if we couldn't skip, bomb out.
  159. }
  160. }
  161. break;
  162. case AMF_DATA_TYPE_DATE:
  163. url_fskip(ioc, 8 + 2); //timestamp (double) and UTC offset (int16)
  164. break;
  165. default: //unsupported type, we couldn't skip
  166. return -1;
  167. }
  168. if(depth == 1 && key) { //only look for metadata values when we are not nested and key != NULL
  169. acodec = astream ? astream->codec : NULL;
  170. vcodec = vstream ? vstream->codec : NULL;
  171. if(amf_type == AMF_DATA_TYPE_BOOL) {
  172. av_metadata_set2(&s->metadata, key, av_d2str(num_val), AV_METADATA_DONT_STRDUP_VAL);
  173. } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
  174. av_metadata_set2(&s->metadata, key, av_d2str(num_val), AV_METADATA_DONT_STRDUP_VAL);
  175. if(!strcmp(key, "duration")) s->duration = num_val * AV_TIME_BASE;
  176. else if(!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
  177. vcodec->bit_rate = num_val * 1024.0;
  178. } else if (amf_type == AMF_DATA_TYPE_STRING)
  179. av_metadata_set2(&s->metadata, key, str,
  180. AV_METADATA_DONT_STRDUP_VAL);
  181. }
  182. return 0;
  183. }
  184. static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) {
  185. AMFDataType type;
  186. AVStream *stream, *astream, *vstream;
  187. ByteIOContext *ioc;
  188. int i;
  189. char *buf = NULL; //only needs to hold the string "onMetaData". Anything longer is something we don't want.
  190. astream = NULL;
  191. vstream = NULL;
  192. ioc = s->pb;
  193. //first object needs to be "onMetaData" string
  194. type = get_byte(ioc);
  195. if (type != AMF_DATA_TYPE_STRING ||
  196. amf_get_string(ioc, &buf) < 0 || strcmp(buf, "onMetaData")) {
  197. av_freep(&buf);
  198. return -1;
  199. }
  200. //find the streams now so that amf_parse_object doesn't need to do the lookup every time it is called.
  201. for(i = 0; i < s->nb_streams; i++) {
  202. stream = s->streams[i];
  203. if (stream->codec->codec_type == CODEC_TYPE_AUDIO) astream = stream;
  204. else if(stream->codec->codec_type == CODEC_TYPE_VIDEO) vstream = stream;
  205. }
  206. //parse the second object (we want a mixed array)
  207. if(amf_parse_object(s, astream, vstream, buf, next_pos, 0) < 0)
  208. return -1;
  209. av_freep(&buf);
  210. return 0;
  211. }
  212. static AVStream *create_stream(AVFormatContext *s, int is_audio){
  213. AVStream *st = av_new_stream(s, is_audio);
  214. if (!st)
  215. return NULL;
  216. st->codec->codec_type = is_audio ? CODEC_TYPE_AUDIO : CODEC_TYPE_VIDEO;
  217. av_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
  218. return st;
  219. }
  220. static int flv_read_header(AVFormatContext *s,
  221. AVFormatParameters *ap)
  222. {
  223. int offset, flags;
  224. url_fskip(s->pb, 4);
  225. flags = get_byte(s->pb);
  226. /* old flvtool cleared this field */
  227. /* FIXME: better fix needed */
  228. if (!flags) {
  229. flags = FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO;
  230. av_log(s, AV_LOG_WARNING, "Broken FLV file, which says no streams present, this might fail\n");
  231. }
  232. if((flags & (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
  233. != (FLV_HEADER_FLAG_HASVIDEO|FLV_HEADER_FLAG_HASAUDIO))
  234. s->ctx_flags |= AVFMTCTX_NOHEADER;
  235. if(flags & FLV_HEADER_FLAG_HASVIDEO){
  236. if(!create_stream(s, 0))
  237. return AVERROR(ENOMEM);
  238. }
  239. if(flags & FLV_HEADER_FLAG_HASAUDIO){
  240. if(!create_stream(s, 1))
  241. return AVERROR(ENOMEM);
  242. }
  243. offset = get_be32(s->pb);
  244. url_fseek(s->pb, offset, SEEK_SET);
  245. s->start_time = 0;
  246. return 0;
  247. }
  248. static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
  249. {
  250. av_free(st->codec->extradata);
  251. st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  252. if (!st->codec->extradata)
  253. return AVERROR(ENOMEM);
  254. st->codec->extradata_size = size;
  255. get_buffer(s->pb, st->codec->extradata, st->codec->extradata_size);
  256. return 0;
  257. }
  258. static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
  259. {
  260. FLVContext *flv = s->priv_data;
  261. int ret, i, type, size, flags, is_audio;
  262. int64_t next, pos;
  263. int64_t dts, pts = AV_NOPTS_VALUE;
  264. AVStream *st = NULL;
  265. for(;;){
  266. pos = url_ftell(s->pb);
  267. url_fskip(s->pb, 4); /* size of previous packet */
  268. type = get_byte(s->pb);
  269. size = get_be24(s->pb);
  270. dts = get_be24(s->pb);
  271. dts |= get_byte(s->pb) << 24;
  272. // av_log(s, AV_LOG_DEBUG, "type:%d, size:%d, dts:%d\n", type, size, dts);
  273. if (url_feof(s->pb))
  274. return AVERROR_EOF;
  275. url_fskip(s->pb, 3); /* stream id, always 0 */
  276. flags = 0;
  277. if(size == 0)
  278. continue;
  279. next= size + url_ftell(s->pb);
  280. if (type == FLV_TAG_TYPE_AUDIO) {
  281. is_audio=1;
  282. flags = get_byte(s->pb);
  283. size--;
  284. } else if (type == FLV_TAG_TYPE_VIDEO) {
  285. is_audio=0;
  286. flags = get_byte(s->pb);
  287. size--;
  288. if ((flags & 0xf0) == 0x50) /* video info / command frame */
  289. goto skip;
  290. } else {
  291. if (type == FLV_TAG_TYPE_META && size > 13+1+4)
  292. flv_read_metabody(s, next);
  293. else /* skip packet */
  294. av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
  295. skip:
  296. url_fseek(s->pb, next, SEEK_SET);
  297. continue;
  298. }
  299. /* skip empty data packets */
  300. if (!size)
  301. continue;
  302. /* now find stream */
  303. for(i=0;i<s->nb_streams;i++) {
  304. st = s->streams[i];
  305. if (st->id == is_audio)
  306. break;
  307. }
  308. if(i == s->nb_streams){
  309. av_log(s, AV_LOG_ERROR, "invalid stream\n");
  310. st= create_stream(s, is_audio);
  311. s->ctx_flags &= ~AVFMTCTX_NOHEADER;
  312. }
  313. // av_log(s, AV_LOG_DEBUG, "%d %X %d \n", is_audio, flags, st->discard);
  314. if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio))
  315. ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
  316. || st->discard >= AVDISCARD_ALL
  317. ){
  318. url_fseek(s->pb, next, SEEK_SET);
  319. continue;
  320. }
  321. if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
  322. av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
  323. break;
  324. }
  325. // if not streamed and no duration from metadata then seek to end to find the duration from the timestamps
  326. if(!url_is_streamed(s->pb) && s->duration==AV_NOPTS_VALUE){
  327. int size;
  328. const int64_t pos= url_ftell(s->pb);
  329. const int64_t fsize= url_fsize(s->pb);
  330. url_fseek(s->pb, fsize-4, SEEK_SET);
  331. size= get_be32(s->pb);
  332. url_fseek(s->pb, fsize-3-size, SEEK_SET);
  333. if(size == get_be24(s->pb) + 11){
  334. uint32_t ts = get_be24(s->pb);
  335. ts |= get_byte(s->pb) << 24;
  336. s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
  337. }
  338. url_fseek(s->pb, pos, SEEK_SET);
  339. }
  340. if(is_audio){
  341. if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
  342. st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
  343. st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
  344. st->codec->bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
  345. }
  346. if(!st->codec->codec_id){
  347. flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);
  348. }
  349. }else{
  350. size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
  351. }
  352. if (st->codec->codec_id == CODEC_ID_AAC ||
  353. st->codec->codec_id == CODEC_ID_H264) {
  354. int type = get_byte(s->pb);
  355. size--;
  356. if (st->codec->codec_id == CODEC_ID_H264) {
  357. int32_t cts = (get_be24(s->pb)+0xff800000)^0xff800000; // sign extension
  358. pts = dts + cts;
  359. if (cts < 0) { // dts are wrong
  360. flv->wrong_dts = 1;
  361. av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
  362. }
  363. if (flv->wrong_dts)
  364. dts = AV_NOPTS_VALUE;
  365. }
  366. if (type == 0) {
  367. if ((ret = flv_get_extradata(s, st, size)) < 0)
  368. return ret;
  369. if (st->codec->codec_id == CODEC_ID_AAC) {
  370. MPEG4AudioConfig cfg;
  371. ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
  372. st->codec->extradata_size);
  373. st->codec->channels = cfg.channels;
  374. st->codec->sample_rate = cfg.sample_rate;
  375. dprintf(s, "mp4a config channels %d sample rate %d\n",
  376. st->codec->channels, st->codec->sample_rate);
  377. }
  378. return AVERROR(EAGAIN);
  379. }
  380. }
  381. /* skip empty data packets */
  382. if (!size)
  383. return AVERROR(EAGAIN);
  384. ret= av_get_packet(s->pb, pkt, size);
  385. if (ret < 0) {
  386. return AVERROR(EIO);
  387. }
  388. /* note: we need to modify the packet size here to handle the last
  389. packet */
  390. pkt->size = ret;
  391. pkt->dts = dts;
  392. pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
  393. pkt->stream_index = st->index;
  394. if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
  395. pkt->flags |= PKT_FLAG_KEY;
  396. return ret;
  397. }
  398. AVInputFormat flv_demuxer = {
  399. "flv",
  400. NULL_IF_CONFIG_SMALL("FLV format"),
  401. sizeof(FLVContext),
  402. flv_probe,
  403. flv_read_header,
  404. flv_read_packet,
  405. .extensions = "flv",
  406. .value = CODEC_ID_FLV1,
  407. };