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.

198 lines
5.8KB

  1. /*
  2. * mtv demuxer
  3. * Copyright (c) 2006 Reynaldo H. Verdejo Pinochet
  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. /**
  22. * @file
  23. * MTV demuxer.
  24. */
  25. #include "libavutil/bswap.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "avformat.h"
  28. #include "internal.h"
  29. #define MTV_ASUBCHUNK_DATA_SIZE 500
  30. #define MTV_HEADER_SIZE 512
  31. #define MTV_AUDIO_PADDING_SIZE 12
  32. #define AUDIO_SAMPLING_RATE 44100
  33. typedef struct MTVDemuxContext {
  34. unsigned int file_size; ///< filesize, not always right
  35. unsigned int segments; ///< number of 512 byte segments
  36. unsigned int audio_identifier; ///< 'MP3' on all files I have seen
  37. unsigned int audio_br; ///< bitrate of audio channel (mp3)
  38. unsigned int img_colorfmt; ///< frame colorfmt rgb 565/555
  39. unsigned int img_bpp; ///< frame bits per pixel
  40. unsigned int img_width; //
  41. unsigned int img_height; //
  42. unsigned int img_segment_size; ///< size of image segment
  43. unsigned int video_fps; //
  44. unsigned int full_segment_size;
  45. } MTVDemuxContext;
  46. static int mtv_probe(AVProbeData *p)
  47. {
  48. /* Magic is 'AMV' */
  49. if (*p->buf != 'A' || *(p->buf + 1) != 'M' || *(p->buf + 2) != 'V')
  50. return 0;
  51. /* Check for nonzero in bpp and (width|height) header fields */
  52. if(!(p->buf[51] && AV_RL16(&p->buf[52]) | AV_RL16(&p->buf[54])))
  53. return 0;
  54. /* If width or height are 0 then imagesize header field should not */
  55. if(!AV_RL16(&p->buf[52]) || !AV_RL16(&p->buf[54]))
  56. {
  57. if(!!AV_RL16(&p->buf[56]))
  58. return AVPROBE_SCORE_MAX/2;
  59. else
  60. return 0;
  61. }
  62. if(p->buf[51] != 16)
  63. return AVPROBE_SCORE_MAX/4; // But we are going to assume 16bpp anyway ..
  64. return AVPROBE_SCORE_MAX;
  65. }
  66. static int mtv_read_header(AVFormatContext *s)
  67. {
  68. MTVDemuxContext *mtv = s->priv_data;
  69. AVIOContext *pb = s->pb;
  70. AVStream *st;
  71. unsigned int audio_subsegments;
  72. avio_skip(pb, 3);
  73. mtv->file_size = avio_rl32(pb);
  74. mtv->segments = avio_rl32(pb);
  75. avio_skip(pb, 32);
  76. mtv->audio_identifier = avio_rl24(pb);
  77. mtv->audio_br = avio_rl16(pb);
  78. mtv->img_colorfmt = avio_rl24(pb);
  79. mtv->img_bpp = avio_r8(pb);
  80. mtv->img_width = avio_rl16(pb);
  81. mtv->img_height = avio_rl16(pb);
  82. mtv->img_segment_size = avio_rl16(pb);
  83. /* Calculate width and height if missing from header */
  84. if(!mtv->img_width)
  85. mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3)
  86. / mtv->img_height;
  87. if(!mtv->img_height)
  88. mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3)
  89. / mtv->img_width;
  90. avio_skip(pb, 4);
  91. audio_subsegments = avio_rl16(pb);
  92. if (audio_subsegments == 0) {
  93. av_log_ask_for_sample(s, "MTV files without audio are not supported\n");
  94. return AVERROR_INVALIDDATA;
  95. }
  96. mtv->full_segment_size =
  97. audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
  98. mtv->img_segment_size;
  99. mtv->video_fps = (mtv->audio_br / 4) / audio_subsegments;
  100. // FIXME Add sanity check here
  101. // all systems go! init decoders
  102. // video - raw rgb565
  103. st = avformat_new_stream(s, NULL);
  104. if(!st)
  105. return AVERROR(ENOMEM);
  106. avpriv_set_pts_info(st, 64, 1, mtv->video_fps);
  107. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  108. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  109. st->codec->pix_fmt = PIX_FMT_RGB565BE;
  110. st->codec->width = mtv->img_width;
  111. st->codec->height = mtv->img_height;
  112. st->codec->sample_rate = mtv->video_fps;
  113. st->codec->extradata = av_strdup("BottomUp");
  114. st->codec->extradata_size = 9;
  115. // audio - mp3
  116. st = avformat_new_stream(s, NULL);
  117. if(!st)
  118. return AVERROR(ENOMEM);
  119. avpriv_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
  120. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  121. st->codec->codec_id = CODEC_ID_MP3;
  122. st->codec->bit_rate = mtv->audio_br;
  123. st->need_parsing = AVSTREAM_PARSE_FULL;
  124. // Jump over header
  125. if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
  126. return AVERROR(EIO);
  127. return 0;
  128. }
  129. static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
  130. {
  131. MTVDemuxContext *mtv = s->priv_data;
  132. AVIOContext *pb = s->pb;
  133. int ret;
  134. if((avio_tell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
  135. {
  136. avio_skip(pb, MTV_AUDIO_PADDING_SIZE);
  137. ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
  138. if(ret < 0)
  139. return ret;
  140. pkt->pos -= MTV_AUDIO_PADDING_SIZE;
  141. pkt->stream_index = 1;
  142. }else
  143. {
  144. ret = av_get_packet(pb, pkt, mtv->img_segment_size);
  145. if(ret < 0)
  146. return ret;
  147. pkt->stream_index = 0;
  148. }
  149. return ret;
  150. }
  151. AVInputFormat ff_mtv_demuxer = {
  152. .name = "mtv",
  153. .long_name = NULL_IF_CONFIG_SMALL("MTV format"),
  154. .priv_data_size = sizeof(MTVDemuxContext),
  155. .read_probe = mtv_probe,
  156. .read_header = mtv_read_header,
  157. .read_packet = mtv_read_packet,
  158. };