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.

200 lines
6.0KB

  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_EXTENSION;
  59. else
  60. return 0;
  61. }
  62. if(p->buf[51] != 16)
  63. return AVPROBE_SCORE_EXTENSION / 2; // 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 && mtv->img_height > 0 && mtv->img_bpp >= 8)
  85. mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3)
  86. / mtv->img_height;
  87. if (!mtv->img_height && mtv->img_width > 0 && mtv->img_bpp >= 8)
  88. mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3)
  89. / mtv->img_width;
  90. if (!mtv->img_width || !mtv->img_height)
  91. return AVERROR_INVALIDDATA;
  92. avio_skip(pb, 4);
  93. audio_subsegments = avio_rl16(pb);
  94. if (audio_subsegments == 0) {
  95. avpriv_request_sample(s, "MTV files without audio");
  96. return AVERROR_PATCHWELCOME;
  97. }
  98. mtv->full_segment_size =
  99. audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
  100. mtv->img_segment_size;
  101. mtv->video_fps = (mtv->audio_br / 4) / audio_subsegments;
  102. // FIXME Add sanity check here
  103. // all systems go! init decoders
  104. // video - raw rgb565
  105. st = avformat_new_stream(s, NULL);
  106. if(!st)
  107. return AVERROR(ENOMEM);
  108. avpriv_set_pts_info(st, 64, 1, mtv->video_fps);
  109. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  110. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  111. st->codecpar->format = AV_PIX_FMT_RGB565BE;
  112. st->codecpar->width = mtv->img_width;
  113. st->codecpar->height = mtv->img_height;
  114. st->codecpar->extradata = av_strdup("BottomUp");
  115. st->codecpar->extradata_size = 9;
  116. // audio - mp3
  117. st = avformat_new_stream(s, NULL);
  118. if(!st)
  119. return AVERROR(ENOMEM);
  120. avpriv_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
  121. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  122. st->codecpar->codec_id = AV_CODEC_ID_MP3;
  123. st->codecpar->bit_rate = mtv->audio_br;
  124. st->need_parsing = AVSTREAM_PARSE_FULL;
  125. // Jump over header
  126. if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
  127. return AVERROR(EIO);
  128. return 0;
  129. }
  130. static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
  131. {
  132. MTVDemuxContext *mtv = s->priv_data;
  133. AVIOContext *pb = s->pb;
  134. int ret;
  135. if((avio_tell(pb) - s->internal->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
  136. {
  137. avio_skip(pb, MTV_AUDIO_PADDING_SIZE);
  138. ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
  139. if(ret < 0)
  140. return ret;
  141. pkt->pos -= MTV_AUDIO_PADDING_SIZE;
  142. pkt->stream_index = 1;
  143. }else
  144. {
  145. ret = av_get_packet(pb, pkt, mtv->img_segment_size);
  146. if(ret < 0)
  147. return ret;
  148. pkt->stream_index = 0;
  149. }
  150. return ret;
  151. }
  152. AVInputFormat ff_mtv_demuxer = {
  153. .name = "mtv",
  154. .long_name = NULL_IF_CONFIG_SMALL("MTV"),
  155. .priv_data_size = sizeof(MTVDemuxContext),
  156. .read_probe = mtv_probe,
  157. .read_header = mtv_read_header,
  158. .read_packet = mtv_read_packet,
  159. };