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.

235 lines
7.0KB

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