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.

231 lines
6.9KB

  1. /*
  2. * mtv demuxer
  3. * Copyright (c) 2006 Reynaldo H. Verdejo Pinochet
  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. /**
  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 MTV_IMAGE_DEFAULT_BPP 16
  33. #define MTV_AUDIO_SAMPLING_RATE 44100
  34. typedef struct MTVDemuxContext {
  35. unsigned int file_size; ///< filesize, not always right
  36. unsigned int segments; ///< number of 512 byte segments
  37. unsigned int audio_identifier; ///< 'MP3' on all files I have seen
  38. unsigned int audio_br; ///< bitrate of audio channel (mp3)
  39. unsigned int img_colorfmt; ///< frame colorfmt rgb 565/555
  40. unsigned int img_bpp; ///< frame bits per pixel
  41. unsigned int img_width;
  42. unsigned int img_height;
  43. unsigned int img_segment_size; ///< size of image segment
  44. unsigned int video_fps;
  45. unsigned int full_segment_size;
  46. } MTVDemuxContext;
  47. static int mtv_probe(AVProbeData *p)
  48. {
  49. /* we need at least 57 bytes from the header
  50. * to try parsing all required fields
  51. */
  52. if (p->buf_size < 57)
  53. return 0;
  54. /* Magic is 'AMV' */
  55. if (*p->buf != 'A' || *(p->buf + 1) != 'M' || *(p->buf + 2) != 'V')
  56. return 0;
  57. /* Check for nonzero in bpp and (width|height) header fields */
  58. if(!(p->buf[51] && AV_RL16(&p->buf[52]) | AV_RL16(&p->buf[54])))
  59. return 0;
  60. /* If width or height are 0 then imagesize header field should not */
  61. if(!AV_RL16(&p->buf[52]) || !AV_RL16(&p->buf[54]))
  62. {
  63. if(!!AV_RL16(&p->buf[56]))
  64. return AVPROBE_SCORE_EXTENSION;
  65. else
  66. return 0;
  67. }
  68. /* Image bpp is not an absolutely required
  69. * field as we latter claim it should be 16
  70. * no matter what. All samples in the wild
  71. * are RGB565/555.
  72. */
  73. if(p->buf[51] != MTV_IMAGE_DEFAULT_BPP)
  74. return AVPROBE_SCORE_EXTENSION / 2;
  75. /* We had enough data to parse header values
  76. * but we expect to be able to get 512 bytes
  77. * of header to be sure.
  78. */
  79. if (p->buf_size < MTV_HEADER_SIZE)
  80. return AVPROBE_SCORE_EXTENSION;
  81. return AVPROBE_SCORE_MAX;
  82. }
  83. static int mtv_read_header(AVFormatContext *s)
  84. {
  85. MTVDemuxContext *mtv = s->priv_data;
  86. AVIOContext *pb = s->pb;
  87. AVStream *st;
  88. unsigned int audio_subsegments;
  89. avio_skip(pb, 3);
  90. mtv->file_size = avio_rl32(pb);
  91. mtv->segments = avio_rl32(pb);
  92. avio_skip(pb, 32);
  93. mtv->audio_identifier = avio_rl24(pb);
  94. mtv->audio_br = avio_rl16(pb);
  95. mtv->img_colorfmt = avio_rl24(pb);
  96. mtv->img_bpp = avio_r8(pb)>>3;
  97. mtv->img_width = avio_rl16(pb);
  98. mtv->img_height = avio_rl16(pb);
  99. mtv->img_segment_size = avio_rl16(pb);
  100. /* Assume 16bpp even if claimed otherwise.
  101. * We know its going to be RGBG565/555 anyway
  102. */
  103. if (mtv->img_bpp != MTV_IMAGE_DEFAULT_BPP) {
  104. av_log (s, AV_LOG_WARNING, "Header claims %dbpp (!= 16). Ignoring\n",
  105. mtv->img_bpp);
  106. mtv->img_bpp = MTV_IMAGE_DEFAULT_BPP;
  107. }
  108. /* Calculate width and height if missing from header */
  109. if(!mtv->img_width && mtv->img_height)
  110. mtv->img_width=mtv->img_segment_size / (mtv->img_bpp)
  111. / mtv->img_height;
  112. if(!mtv->img_height && mtv->img_width)
  113. mtv->img_height=mtv->img_segment_size / (mtv->img_bpp)
  114. / mtv->img_width;
  115. if(!mtv->img_height || !mtv->img_width || !mtv->img_segment_size){
  116. av_log(s, AV_LOG_ERROR, "width or height or segment_size is invalid and I cannot calculate them from other information\n");
  117. return AVERROR(EINVAL);
  118. }
  119. avio_skip(pb, 4);
  120. audio_subsegments = avio_rl16(pb);
  121. if (audio_subsegments == 0) {
  122. avpriv_request_sample(s, "MTV files without audio");
  123. return AVERROR_PATCHWELCOME;
  124. }
  125. mtv->full_segment_size =
  126. audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
  127. mtv->img_segment_size;
  128. mtv->video_fps = (mtv->audio_br / 4) / audio_subsegments;
  129. // FIXME Add sanity check here
  130. // all systems go! init decoders
  131. // video - raw rgb565
  132. st = avformat_new_stream(s, NULL);
  133. if(!st)
  134. return AVERROR(ENOMEM);
  135. avpriv_set_pts_info(st, 64, 1, mtv->video_fps);
  136. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  137. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  138. st->codec->pix_fmt = AV_PIX_FMT_RGB565BE;
  139. st->codec->width = mtv->img_width;
  140. st->codec->height = mtv->img_height;
  141. st->codec->sample_rate = mtv->video_fps;
  142. st->codec->extradata = av_strdup("BottomUp");
  143. st->codec->extradata_size = 9;
  144. // audio - mp3
  145. st = avformat_new_stream(s, NULL);
  146. if(!st)
  147. return AVERROR(ENOMEM);
  148. avpriv_set_pts_info(st, 64, 1, MTV_AUDIO_SAMPLING_RATE);
  149. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  150. st->codec->codec_id = AV_CODEC_ID_MP3;
  151. st->codec->bit_rate = mtv->audio_br;
  152. st->need_parsing = AVSTREAM_PARSE_FULL;
  153. // Jump over header
  154. if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
  155. return AVERROR(EIO);
  156. return 0;
  157. }
  158. static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
  159. {
  160. MTVDemuxContext *mtv = s->priv_data;
  161. AVIOContext *pb = s->pb;
  162. int ret;
  163. if((avio_tell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
  164. {
  165. avio_skip(pb, MTV_AUDIO_PADDING_SIZE);
  166. ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
  167. if(ret < 0)
  168. return ret;
  169. pkt->pos -= MTV_AUDIO_PADDING_SIZE;
  170. pkt->stream_index = 1;
  171. }else
  172. {
  173. ret = av_get_packet(pb, pkt, mtv->img_segment_size);
  174. if(ret < 0)
  175. return ret;
  176. pkt->stream_index = 0;
  177. }
  178. return ret;
  179. }
  180. AVInputFormat ff_mtv_demuxer = {
  181. .name = "mtv",
  182. .long_name = NULL_IF_CONFIG_SMALL("MTV"),
  183. .priv_data_size = sizeof(MTVDemuxContext),
  184. .read_probe = mtv_probe,
  185. .read_header = mtv_read_header,
  186. .read_packet = mtv_read_packet,
  187. };