mvi.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Motion Pixels MVI Demuxer
  3. * Copyright (c) 2008 Gregory Montoir (cyx@users.sourceforge.net)
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "internal.h"
  23. #define MVI_FRAC_BITS 10
  24. #define MVI_AUDIO_STREAM_INDEX 0
  25. #define MVI_VIDEO_STREAM_INDEX 1
  26. typedef struct MviDemuxContext {
  27. unsigned int (*get_int)(AVIOContext *);
  28. uint32_t audio_data_size;
  29. uint64_t audio_size_counter;
  30. uint64_t audio_frame_size;
  31. int audio_size_left;
  32. int video_frame_size;
  33. } MviDemuxContext;
  34. static int read_header(AVFormatContext *s, AVFormatParameters *ap)
  35. {
  36. MviDemuxContext *mvi = s->priv_data;
  37. AVIOContext *pb = s->pb;
  38. AVStream *ast, *vst;
  39. unsigned int version, frames_count, msecs_per_frame, player_version;
  40. ast = avformat_new_stream(s, NULL);
  41. if (!ast)
  42. return AVERROR(ENOMEM);
  43. vst = avformat_new_stream(s, NULL);
  44. if (!vst)
  45. return AVERROR(ENOMEM);
  46. vst->codec->extradata_size = 2;
  47. vst->codec->extradata = av_mallocz(2 + FF_INPUT_BUFFER_PADDING_SIZE);
  48. version = avio_r8(pb);
  49. vst->codec->extradata[0] = avio_r8(pb);
  50. vst->codec->extradata[1] = avio_r8(pb);
  51. frames_count = avio_rl32(pb);
  52. msecs_per_frame = avio_rl32(pb);
  53. vst->codec->width = avio_rl16(pb);
  54. vst->codec->height = avio_rl16(pb);
  55. avio_r8(pb);
  56. ast->codec->sample_rate = avio_rl16(pb);
  57. mvi->audio_data_size = avio_rl32(pb);
  58. avio_r8(pb);
  59. player_version = avio_rl32(pb);
  60. avio_rl16(pb);
  61. avio_r8(pb);
  62. if (frames_count == 0 || mvi->audio_data_size == 0)
  63. return AVERROR_INVALIDDATA;
  64. if (version != 7 || player_version > 213) {
  65. av_log(s, AV_LOG_ERROR, "unhandled version (%d,%d)\n", version, player_version);
  66. return AVERROR_INVALIDDATA;
  67. }
  68. avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  69. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  70. ast->codec->codec_id = CODEC_ID_PCM_U8;
  71. ast->codec->channels = 1;
  72. ast->codec->bits_per_coded_sample = 8;
  73. ast->codec->bit_rate = ast->codec->sample_rate * 8;
  74. avpriv_set_pts_info(vst, 64, msecs_per_frame, 1000000);
  75. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  76. vst->codec->codec_id = CODEC_ID_MOTIONPIXELS;
  77. mvi->get_int = (vst->codec->width * vst->codec->height < (1 << 16)) ? avio_rl16 : avio_rl24;
  78. mvi->audio_frame_size = ((uint64_t)mvi->audio_data_size << MVI_FRAC_BITS) / frames_count;
  79. if (mvi->audio_frame_size <= 1 << MVI_FRAC_BITS - 1) {
  80. av_log(s, AV_LOG_ERROR, "Invalid audio_data_size (%d) or frames_count (%d)\n",
  81. mvi->audio_data_size, frames_count);
  82. return AVERROR_INVALIDDATA;
  83. }
  84. mvi->audio_size_counter = (ast->codec->sample_rate * 830 / mvi->audio_frame_size - 1) * mvi->audio_frame_size;
  85. mvi->audio_size_left = mvi->audio_data_size;
  86. return 0;
  87. }
  88. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  89. {
  90. int ret, count;
  91. MviDemuxContext *mvi = s->priv_data;
  92. AVIOContext *pb = s->pb;
  93. if (mvi->video_frame_size == 0) {
  94. mvi->video_frame_size = (mvi->get_int)(pb);
  95. if (mvi->audio_size_left == 0)
  96. return AVERROR(EIO);
  97. count = (mvi->audio_size_counter + mvi->audio_frame_size + 512) >> MVI_FRAC_BITS;
  98. if (count > mvi->audio_size_left)
  99. count = mvi->audio_size_left;
  100. if ((ret = av_get_packet(pb, pkt, count)) < 0)
  101. return ret;
  102. pkt->stream_index = MVI_AUDIO_STREAM_INDEX;
  103. mvi->audio_size_left -= count;
  104. mvi->audio_size_counter += mvi->audio_frame_size - (count << MVI_FRAC_BITS);
  105. } else {
  106. if ((ret = av_get_packet(pb, pkt, mvi->video_frame_size)) < 0)
  107. return ret;
  108. pkt->stream_index = MVI_VIDEO_STREAM_INDEX;
  109. mvi->video_frame_size = 0;
  110. }
  111. return 0;
  112. }
  113. AVInputFormat ff_mvi_demuxer = {
  114. .name = "mvi",
  115. .long_name = NULL_IF_CONFIG_SMALL("Motion Pixels MVI format"),
  116. .priv_data_size = sizeof(MviDemuxContext),
  117. .read_header = read_header,
  118. .read_packet = read_packet,
  119. .extensions = "mvi"
  120. };