mvi.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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)(ByteIOContext *);
  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. ByteIOContext *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 = get_byte(pb);
  48. vst->codec->extradata[0] = get_byte(pb);
  49. vst->codec->extradata[1] = get_byte(pb);
  50. frames_count = get_le32(pb);
  51. msecs_per_frame = get_le32(pb);
  52. vst->codec->width = get_le16(pb);
  53. vst->codec->height = get_le16(pb);
  54. get_byte(pb);
  55. ast->codec->sample_rate = get_le16(pb);
  56. mvi->audio_data_size = get_le32(pb);
  57. get_byte(pb);
  58. player_version = get_le32(pb);
  59. get_le16(pb);
  60. get_byte(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 = CODEC_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 = CODEC_TYPE_VIDEO;
  75. vst->codec->codec_id = CODEC_ID_MOTIONPIXELS;
  76. vst->codec->pix_fmt = PIX_FMT_RGB555;
  77. mvi->get_int = (vst->codec->width * vst->codec->height < (1 << 16)) ? get_le16 : get_le24;
  78. mvi->audio_frame_size = ((uint64_t)mvi->audio_data_size << MVI_FRAC_BITS) / frames_count;
  79. mvi->audio_size_counter = (ast->codec->sample_rate * 830 / mvi->audio_frame_size - 1) * mvi->audio_frame_size;
  80. mvi->audio_size_left = mvi->audio_data_size;
  81. return 0;
  82. }
  83. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  84. {
  85. int ret, count;
  86. MviDemuxContext *mvi = s->priv_data;
  87. ByteIOContext *pb = s->pb;
  88. if (mvi->video_frame_size == 0) {
  89. mvi->video_frame_size = (mvi->get_int)(pb);
  90. if (mvi->audio_size_left == 0)
  91. return AVERROR(EIO);
  92. count = (mvi->audio_size_counter + mvi->audio_frame_size + 512) >> MVI_FRAC_BITS;
  93. if (count > mvi->audio_size_left)
  94. count = mvi->audio_size_left;
  95. if ((ret = av_get_packet(pb, pkt, count)) < 0)
  96. return ret;
  97. pkt->stream_index = MVI_AUDIO_STREAM_INDEX;
  98. mvi->audio_size_left -= count;
  99. mvi->audio_size_counter += mvi->audio_frame_size - (count << MVI_FRAC_BITS);
  100. } else {
  101. if ((ret = av_get_packet(pb, pkt, mvi->video_frame_size)) < 0)
  102. return ret;
  103. pkt->stream_index = MVI_VIDEO_STREAM_INDEX;
  104. mvi->video_frame_size = 0;
  105. }
  106. return 0;
  107. }
  108. AVInputFormat mvi_demuxer = {
  109. "mvi",
  110. NULL_IF_CONFIG_SMALL("Motion Pixels MVI format"),
  111. sizeof(MviDemuxContext),
  112. NULL,
  113. read_header,
  114. read_packet,
  115. .extensions = "mvi"
  116. };