mtv.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 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_size < 57 || !(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_bpp>>3){
  85. if(!mtv->img_width && mtv->img_height)
  86. mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3)
  87. / mtv->img_height;
  88. if(!mtv->img_height && mtv->img_width)
  89. mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3)
  90. / mtv->img_width;
  91. }
  92. if(!mtv->img_height || !mtv->img_width){
  93. av_log(s, AV_LOG_ERROR, "width or height is invalid and I cannot calculate them from other information\n");
  94. return AVERROR(EINVAL);
  95. }
  96. avio_skip(pb, 4);
  97. audio_subsegments = avio_rl16(pb);
  98. if (audio_subsegments == 0) {
  99. av_log_ask_for_sample(s, "MTV files without audio are not supported\n");
  100. return AVERROR_INVALIDDATA;
  101. }
  102. mtv->full_segment_size =
  103. audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
  104. mtv->img_segment_size;
  105. mtv->video_fps = (mtv->audio_br / 4) / audio_subsegments;
  106. // FIXME Add sanity check here
  107. // all systems go! init decoders
  108. // video - raw rgb565
  109. st = avformat_new_stream(s, NULL);
  110. if(!st)
  111. return AVERROR(ENOMEM);
  112. avpriv_set_pts_info(st, 64, 1, mtv->video_fps);
  113. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  114. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  115. st->codec->pix_fmt = PIX_FMT_RGB565BE;
  116. st->codec->width = mtv->img_width;
  117. st->codec->height = mtv->img_height;
  118. st->codec->sample_rate = mtv->video_fps;
  119. st->codec->extradata = av_strdup("BottomUp");
  120. st->codec->extradata_size = 9;
  121. // audio - mp3
  122. st = avformat_new_stream(s, NULL);
  123. if(!st)
  124. return AVERROR(ENOMEM);
  125. avpriv_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
  126. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  127. st->codec->codec_id = AV_CODEC_ID_MP3;
  128. st->codec->bit_rate = mtv->audio_br;
  129. st->need_parsing = AVSTREAM_PARSE_FULL;
  130. // Jump over header
  131. if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
  132. return AVERROR(EIO);
  133. return 0;
  134. }
  135. static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
  136. {
  137. MTVDemuxContext *mtv = s->priv_data;
  138. AVIOContext *pb = s->pb;
  139. int ret;
  140. if((avio_tell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
  141. {
  142. avio_skip(pb, MTV_AUDIO_PADDING_SIZE);
  143. ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
  144. if(ret < 0)
  145. return ret;
  146. pkt->pos -= MTV_AUDIO_PADDING_SIZE;
  147. pkt->stream_index = 1;
  148. }else
  149. {
  150. ret = av_get_packet(pb, pkt, mtv->img_segment_size);
  151. if(ret < 0)
  152. return ret;
  153. pkt->stream_index = 0;
  154. }
  155. return ret;
  156. }
  157. AVInputFormat ff_mtv_demuxer = {
  158. .name = "mtv",
  159. .long_name = NULL_IF_CONFIG_SMALL("MTV"),
  160. .priv_data_size = sizeof(MTVDemuxContext),
  161. .read_probe = mtv_probe,
  162. .read_header = mtv_read_header,
  163. .read_packet = mtv_read_packet,
  164. };