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.

150 lines
5.1KB

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