mtv.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. // audio - mp3
  91. st = av_new_stream(s, AUDIO_SID);
  92. if(!st)
  93. return AVERROR(ENOMEM);
  94. av_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
  95. st->codec->codec_type = CODEC_TYPE_AUDIO;
  96. st->codec->codec_id = CODEC_ID_MP3;
  97. st->codec->bit_rate = mtv->audio_br;
  98. st->need_parsing = AVSTREAM_PARSE_FULL;
  99. // Jump over header
  100. if(url_fseek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
  101. return AVERROR(EIO);
  102. return 0;
  103. }
  104. static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
  105. {
  106. MTVDemuxContext *mtv = s->priv_data;
  107. ByteIOContext *pb = s->pb;
  108. int ret;
  109. #ifndef WORDS_BIGENDIAN
  110. int i;
  111. #endif
  112. ret = 0;
  113. if((url_ftell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
  114. {
  115. url_fskip(pb, MTV_AUDIO_PADDING_SIZE);
  116. ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
  117. if(ret != MTV_ASUBCHUNK_DATA_SIZE)
  118. return AVERROR(EIO);
  119. pkt->pos -= MTV_AUDIO_PADDING_SIZE;
  120. pkt->stream_index = AUDIO_SID;
  121. }else
  122. {
  123. ret = av_get_packet(pb, pkt, mtv->img_segment_size);
  124. if(ret != mtv->img_segment_size)
  125. return AVERROR(EIO);
  126. #ifndef WORDS_BIGENDIAN
  127. /* pkt->data is GGGRRRR BBBBBGGG
  128. * and we need RRRRRGGG GGGBBBBB
  129. * for PIX_FMT_RGB565 so here we
  130. * just swap bytes as they come
  131. */
  132. for(i=0;i<mtv->img_segment_size/2;i++)
  133. *((uint16_t *)pkt->data+i) = bswap_16(*((uint16_t *)pkt->data+i));
  134. #endif
  135. pkt->stream_index = VIDEO_SID;
  136. }
  137. return ret;
  138. }
  139. AVInputFormat mtv_demuxer = {
  140. "MTV",
  141. NULL_IF_CONFIG_SMALL("MTV format"),
  142. sizeof(MTVDemuxContext),
  143. mtv_probe,
  144. mtv_read_header,
  145. mtv_read_packet,
  146. };