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.

137 lines
4.6KB

  1. /*
  2. * Motion Pixels MVI Demuxer
  3. * Copyright (c) 2008 Gregory Montoir (cyx@users.sourceforge.net)
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/channel_layout.h"
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #define MVI_FRAC_BITS 10
  25. #define MVI_AUDIO_STREAM_INDEX 0
  26. #define MVI_VIDEO_STREAM_INDEX 1
  27. typedef struct MviDemuxContext {
  28. unsigned int (*get_int)(AVIOContext *);
  29. uint32_t audio_data_size;
  30. uint64_t audio_size_counter;
  31. uint64_t audio_frame_size;
  32. int audio_size_left;
  33. int video_frame_size;
  34. } MviDemuxContext;
  35. static int read_header(AVFormatContext *s)
  36. {
  37. MviDemuxContext *mvi = s->priv_data;
  38. AVIOContext *pb = s->pb;
  39. AVStream *ast, *vst;
  40. unsigned int version, frames_count, msecs_per_frame, player_version;
  41. ast = avformat_new_stream(s, NULL);
  42. if (!ast)
  43. return AVERROR(ENOMEM);
  44. vst = avformat_new_stream(s, NULL);
  45. if (!vst)
  46. return AVERROR(ENOMEM);
  47. vst->codec->extradata_size = 2;
  48. vst->codec->extradata = av_mallocz(2 + FF_INPUT_BUFFER_PADDING_SIZE);
  49. version = avio_r8(pb);
  50. vst->codec->extradata[0] = avio_r8(pb);
  51. vst->codec->extradata[1] = avio_r8(pb);
  52. frames_count = avio_rl32(pb);
  53. msecs_per_frame = avio_rl32(pb);
  54. vst->codec->width = avio_rl16(pb);
  55. vst->codec->height = avio_rl16(pb);
  56. avio_r8(pb);
  57. ast->codec->sample_rate = avio_rl16(pb);
  58. mvi->audio_data_size = avio_rl32(pb);
  59. avio_r8(pb);
  60. player_version = avio_rl32(pb);
  61. avio_rl16(pb);
  62. avio_r8(pb);
  63. if (frames_count == 0 || mvi->audio_data_size == 0)
  64. return AVERROR_INVALIDDATA;
  65. if (version != 7 || player_version > 213) {
  66. av_log(s, AV_LOG_ERROR, "unhandled version (%d,%d)\n", version, player_version);
  67. return AVERROR_INVALIDDATA;
  68. }
  69. avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  70. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  71. ast->codec->codec_id = AV_CODEC_ID_PCM_U8;
  72. ast->codec->channels = 1;
  73. ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
  74. ast->codec->bits_per_coded_sample = 8;
  75. ast->codec->bit_rate = ast->codec->sample_rate * 8;
  76. avpriv_set_pts_info(vst, 64, msecs_per_frame, 1000000);
  77. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  78. vst->codec->codec_id = AV_CODEC_ID_MOTIONPIXELS;
  79. mvi->get_int = (vst->codec->width * vst->codec->height < (1 << 16)) ? avio_rl16 : avio_rl24;
  80. mvi->audio_frame_size = ((uint64_t)mvi->audio_data_size << MVI_FRAC_BITS) / frames_count;
  81. mvi->audio_size_counter = (ast->codec->sample_rate * 830 / mvi->audio_frame_size - 1) * mvi->audio_frame_size;
  82. mvi->audio_size_left = mvi->audio_data_size;
  83. return 0;
  84. }
  85. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  86. {
  87. int ret, count;
  88. MviDemuxContext *mvi = s->priv_data;
  89. AVIOContext *pb = s->pb;
  90. if (mvi->video_frame_size == 0) {
  91. mvi->video_frame_size = (mvi->get_int)(pb);
  92. if (mvi->audio_size_left == 0)
  93. return AVERROR(EIO);
  94. count = (mvi->audio_size_counter + mvi->audio_frame_size + 512) >> MVI_FRAC_BITS;
  95. if (count > mvi->audio_size_left)
  96. count = mvi->audio_size_left;
  97. if ((ret = av_get_packet(pb, pkt, count)) < 0)
  98. return ret;
  99. pkt->stream_index = MVI_AUDIO_STREAM_INDEX;
  100. mvi->audio_size_left -= count;
  101. mvi->audio_size_counter += mvi->audio_frame_size - (count << MVI_FRAC_BITS);
  102. } else {
  103. if ((ret = av_get_packet(pb, pkt, mvi->video_frame_size)) < 0)
  104. return ret;
  105. pkt->stream_index = MVI_VIDEO_STREAM_INDEX;
  106. mvi->video_frame_size = 0;
  107. }
  108. return 0;
  109. }
  110. AVInputFormat ff_mvi_demuxer = {
  111. .name = "mvi",
  112. .long_name = NULL_IF_CONFIG_SMALL("Motion Pixels MVI"),
  113. .priv_data_size = sizeof(MviDemuxContext),
  114. .read_header = read_header,
  115. .read_packet = read_packet,
  116. .extensions = "mvi",
  117. };