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.

180 lines
5.2KB

  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 libavformat/mtv.c
  23. * MTV demuxer.
  24. */
  25. #include "libavutil/bswap.h"
  26. #include "avformat.h"
  27. #define MTV_ASUBCHUNK_DATA_SIZE 500
  28. #define MTV_HEADER_SIZE 512
  29. #define MTV_AUDIO_PADDING_SIZE 12
  30. #define AUDIO_SAMPLING_RATE 44100
  31. #define VIDEO_SID 0
  32. #define AUDIO_SID 1
  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 chanel (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. return AVPROBE_SCORE_MAX;
  52. }
  53. static int mtv_read_header(AVFormatContext *s, AVFormatParameters *ap)
  54. {
  55. MTVDemuxContext *mtv = s->priv_data;
  56. ByteIOContext *pb = s->pb;
  57. AVStream *st;
  58. unsigned int audio_subsegments;
  59. url_fskip(pb, 3);
  60. mtv->file_size = get_le32(pb);
  61. mtv->segments = get_le32(pb);
  62. url_fskip(pb, 32);
  63. mtv->audio_identifier = get_le24(pb);
  64. mtv->audio_br = get_le16(pb);
  65. mtv->img_colorfmt = get_le24(pb);
  66. mtv->img_bpp = get_byte(pb);
  67. mtv->img_width = get_le16(pb);
  68. mtv->img_height = get_le16(pb);
  69. mtv->img_segment_size = get_le16(pb);
  70. url_fskip(pb, 4);
  71. audio_subsegments = get_le16(pb);
  72. mtv->full_segment_size =
  73. audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
  74. mtv->img_segment_size;
  75. mtv->video_fps = (mtv->audio_br / 4) / audio_subsegments;
  76. // FIXME Add sanity check here
  77. // all systems go! init decoders
  78. // video - raw rgb565
  79. st = av_new_stream(s, VIDEO_SID);
  80. if(!st)
  81. return AVERROR(ENOMEM);
  82. av_set_pts_info(st, 64, 1, mtv->video_fps);
  83. st->codec->codec_type = CODEC_TYPE_VIDEO;
  84. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  85. st->codec->codec_tag = MKTAG('R', 'G', 'B', mtv->img_bpp);
  86. st->codec->width = mtv->img_width;
  87. st->codec->height = mtv->img_height;
  88. st->codec->bits_per_coded_sample = mtv->img_bpp;
  89. st->codec->sample_rate = mtv->video_fps;
  90. st->codec->extradata = av_strdup("BottomUp");
  91. st->codec->extradata_size = 9;
  92. // audio - mp3
  93. st = av_new_stream(s, AUDIO_SID);
  94. if(!st)
  95. return AVERROR(ENOMEM);
  96. av_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
  97. st->codec->codec_type = CODEC_TYPE_AUDIO;
  98. st->codec->codec_id = CODEC_ID_MP3;
  99. st->codec->bit_rate = mtv->audio_br;
  100. st->need_parsing = AVSTREAM_PARSE_FULL;
  101. // Jump over header
  102. if(url_fseek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
  103. return AVERROR(EIO);
  104. return 0;
  105. }
  106. static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
  107. {
  108. MTVDemuxContext *mtv = s->priv_data;
  109. ByteIOContext *pb = s->pb;
  110. int ret;
  111. #if !HAVE_BIGENDIAN
  112. int i;
  113. #endif
  114. if((url_ftell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
  115. {
  116. url_fskip(pb, MTV_AUDIO_PADDING_SIZE);
  117. ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
  118. if(ret != MTV_ASUBCHUNK_DATA_SIZE)
  119. return AVERROR(EIO);
  120. pkt->pos -= MTV_AUDIO_PADDING_SIZE;
  121. pkt->stream_index = AUDIO_SID;
  122. }else
  123. {
  124. ret = av_get_packet(pb, pkt, mtv->img_segment_size);
  125. if(ret != mtv->img_segment_size)
  126. return AVERROR(EIO);
  127. #if !HAVE_BIGENDIAN
  128. /* pkt->data is GGGRRRR BBBBBGGG
  129. * and we need RRRRRGGG GGGBBBBB
  130. * for PIX_FMT_RGB565 so here we
  131. * just swap bytes as they come
  132. */
  133. for(i=0;i<mtv->img_segment_size/2;i++)
  134. *((uint16_t *)pkt->data+i) = bswap_16(*((uint16_t *)pkt->data+i));
  135. #endif
  136. pkt->stream_index = VIDEO_SID;
  137. }
  138. return ret;
  139. }
  140. AVInputFormat mtv_demuxer = {
  141. "MTV",
  142. NULL_IF_CONFIG_SMALL("MTV format"),
  143. sizeof(MTVDemuxContext),
  144. mtv_probe,
  145. mtv_read_header,
  146. mtv_read_packet,
  147. };