mvi.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #define MVI_FRAC_BITS 10
  23. #define MVI_AUDIO_STREAM_INDEX 0
  24. #define MVI_VIDEO_STREAM_INDEX 1
  25. typedef struct MviDemuxContext {
  26. unsigned int (*get_int)(AVIOContext *);
  27. uint32_t audio_data_size;
  28. uint64_t audio_size_counter;
  29. uint64_t audio_frame_size;
  30. int audio_size_left;
  31. int video_frame_size;
  32. } MviDemuxContext;
  33. static int read_header(AVFormatContext *s, AVFormatParameters *ap)
  34. {
  35. MviDemuxContext *mvi = s->priv_data;
  36. AVIOContext *pb = s->pb;
  37. AVStream *ast, *vst;
  38. unsigned int version, frames_count, msecs_per_frame, player_version;
  39. ast = av_new_stream(s, 0);
  40. if (!ast)
  41. return AVERROR(ENOMEM);
  42. vst = av_new_stream(s, 0);
  43. if (!vst)
  44. return AVERROR(ENOMEM);
  45. vst->codec->extradata_size = 2;
  46. vst->codec->extradata = av_mallocz(2 + FF_INPUT_BUFFER_PADDING_SIZE);
  47. version = avio_r8(pb);
  48. vst->codec->extradata[0] = avio_r8(pb);
  49. vst->codec->extradata[1] = avio_r8(pb);
  50. frames_count = avio_rl32(pb);
  51. msecs_per_frame = avio_rl32(pb);
  52. vst->codec->width = avio_rl16(pb);
  53. vst->codec->height = avio_rl16(pb);
  54. avio_r8(pb);
  55. ast->codec->sample_rate = avio_rl16(pb);
  56. mvi->audio_data_size = avio_rl32(pb);
  57. avio_r8(pb);
  58. player_version = avio_rl32(pb);
  59. avio_rl16(pb);
  60. avio_r8(pb);
  61. if (frames_count == 0 || mvi->audio_data_size == 0)
  62. return AVERROR_INVALIDDATA;
  63. if (version != 7 || player_version > 213) {
  64. av_log(s, AV_LOG_ERROR, "unhandled version (%d,%d)\n", version, player_version);
  65. return AVERROR_INVALIDDATA;
  66. }
  67. av_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  68. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  69. ast->codec->codec_id = CODEC_ID_PCM_U8;
  70. ast->codec->channels = 1;
  71. ast->codec->bits_per_coded_sample = 8;
  72. ast->codec->bit_rate = ast->codec->sample_rate * 8;
  73. av_set_pts_info(vst, 64, msecs_per_frame, 1000000);
  74. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  75. vst->codec->codec_id = CODEC_ID_MOTIONPIXELS;
  76. mvi->get_int = (vst->codec->width * vst->codec->height < (1 << 16)) ? avio_rl16 : avio_rl24;
  77. mvi->audio_frame_size = ((uint64_t)mvi->audio_data_size << MVI_FRAC_BITS) / frames_count;
  78. mvi->audio_size_counter = (ast->codec->sample_rate * 830 / mvi->audio_frame_size - 1) * mvi->audio_frame_size;
  79. mvi->audio_size_left = mvi->audio_data_size;
  80. return 0;
  81. }
  82. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  83. {
  84. int ret, count;
  85. MviDemuxContext *mvi = s->priv_data;
  86. AVIOContext *pb = s->pb;
  87. if (mvi->video_frame_size == 0) {
  88. mvi->video_frame_size = (mvi->get_int)(pb);
  89. if (mvi->audio_size_left == 0)
  90. return AVERROR(EIO);
  91. count = (mvi->audio_size_counter + mvi->audio_frame_size + 512) >> MVI_FRAC_BITS;
  92. if (count > mvi->audio_size_left)
  93. count = mvi->audio_size_left;
  94. if ((ret = av_get_packet(pb, pkt, count)) < 0)
  95. return ret;
  96. pkt->stream_index = MVI_AUDIO_STREAM_INDEX;
  97. mvi->audio_size_left -= count;
  98. mvi->audio_size_counter += mvi->audio_frame_size - (count << MVI_FRAC_BITS);
  99. } else {
  100. if ((ret = av_get_packet(pb, pkt, mvi->video_frame_size)) < 0)
  101. return ret;
  102. pkt->stream_index = MVI_VIDEO_STREAM_INDEX;
  103. mvi->video_frame_size = 0;
  104. }
  105. return 0;
  106. }
  107. AVInputFormat ff_mvi_demuxer = {
  108. .name = "mvi",
  109. .long_name = NULL_IF_CONFIG_SMALL("Motion Pixels MVI format"),
  110. .priv_data_size = sizeof(MviDemuxContext),
  111. .read_header = read_header,
  112. .read_packet = read_packet,
  113. .extensions = "mvi"
  114. };