idroqdec.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * id RoQ (.roq) File Demuxer
  3. * Copyright (c) 2003 The ffmpeg Project
  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. * id RoQ format file demuxer
  24. * by Mike Melanson (melanson@pcisys.net)
  25. * for more information on the .roq file format, visit:
  26. * http://www.csse.monash.edu.au/~timf/
  27. */
  28. #include "libavutil/intreadwrite.h"
  29. #include "avformat.h"
  30. #define RoQ_MAGIC_NUMBER 0x1084
  31. #define RoQ_CHUNK_PREAMBLE_SIZE 8
  32. #define RoQ_AUDIO_SAMPLE_RATE 22050
  33. #define RoQ_CHUNKS_TO_SCAN 30
  34. #define RoQ_INFO 0x1001
  35. #define RoQ_QUAD_CODEBOOK 0x1002
  36. #define RoQ_QUAD_VQ 0x1011
  37. #define RoQ_SOUND_MONO 0x1020
  38. #define RoQ_SOUND_STEREO 0x1021
  39. typedef struct RoqDemuxContext {
  40. int width;
  41. int height;
  42. int audio_channels;
  43. int video_stream_index;
  44. int audio_stream_index;
  45. int64_t video_pts;
  46. unsigned int audio_frame_count;
  47. } RoqDemuxContext;
  48. static int roq_probe(AVProbeData *p)
  49. {
  50. if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
  51. (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
  52. return 0;
  53. return AVPROBE_SCORE_MAX;
  54. }
  55. static int roq_read_header(AVFormatContext *s,
  56. AVFormatParameters *ap)
  57. {
  58. RoqDemuxContext *roq = s->priv_data;
  59. AVIOContext *pb = s->pb;
  60. int framerate;
  61. AVStream *st;
  62. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  63. /* get the main header */
  64. if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  65. RoQ_CHUNK_PREAMBLE_SIZE)
  66. return AVERROR(EIO);
  67. framerate = AV_RL16(&preamble[6]);
  68. /* init private context parameters */
  69. roq->width = roq->height = roq->audio_channels = roq->video_pts =
  70. roq->audio_frame_count = 0;
  71. roq->audio_stream_index = -1;
  72. st = av_new_stream(s, 0);
  73. if (!st)
  74. return AVERROR(ENOMEM);
  75. av_set_pts_info(st, 63, 1, framerate);
  76. roq->video_stream_index = st->index;
  77. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  78. st->codec->codec_id = CODEC_ID_ROQ;
  79. st->codec->codec_tag = 0; /* no fourcc */
  80. return 0;
  81. }
  82. static int roq_read_packet(AVFormatContext *s,
  83. AVPacket *pkt)
  84. {
  85. RoqDemuxContext *roq = s->priv_data;
  86. AVIOContext *pb = s->pb;
  87. int ret = 0;
  88. unsigned int chunk_size;
  89. unsigned int chunk_type;
  90. unsigned int codebook_size;
  91. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  92. int packet_read = 0;
  93. int64_t codebook_offset;
  94. while (!packet_read) {
  95. if (url_feof(s->pb))
  96. return AVERROR(EIO);
  97. /* get the next chunk preamble */
  98. if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
  99. RoQ_CHUNK_PREAMBLE_SIZE)
  100. return AVERROR(EIO);
  101. chunk_type = AV_RL16(&preamble[0]);
  102. chunk_size = AV_RL32(&preamble[2]);
  103. if(chunk_size > INT_MAX)
  104. return AVERROR_INVALIDDATA;
  105. switch (chunk_type) {
  106. case RoQ_INFO:
  107. if (!roq->width || !roq->height) {
  108. AVStream *st = s->streams[roq->video_stream_index];
  109. if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != RoQ_CHUNK_PREAMBLE_SIZE)
  110. return AVERROR(EIO);
  111. st->codec->width = roq->width = AV_RL16(preamble);
  112. st->codec->height = roq->height = AV_RL16(preamble + 2);
  113. break;
  114. }
  115. /* don't care about this chunk anymore */
  116. avio_skip(pb, RoQ_CHUNK_PREAMBLE_SIZE);
  117. break;
  118. case RoQ_QUAD_CODEBOOK:
  119. /* packet needs to contain both this codebook and next VQ chunk */
  120. codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
  121. codebook_size = chunk_size;
  122. avio_skip(pb, codebook_size);
  123. if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  124. RoQ_CHUNK_PREAMBLE_SIZE)
  125. return AVERROR(EIO);
  126. chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
  127. codebook_size;
  128. /* rewind */
  129. avio_seek(pb, codebook_offset, SEEK_SET);
  130. /* load up the packet */
  131. ret= av_get_packet(pb, pkt, chunk_size);
  132. if (ret != chunk_size)
  133. return AVERROR(EIO);
  134. pkt->stream_index = roq->video_stream_index;
  135. pkt->pts = roq->video_pts++;
  136. packet_read = 1;
  137. break;
  138. case RoQ_SOUND_MONO:
  139. case RoQ_SOUND_STEREO:
  140. if (roq->audio_stream_index == -1) {
  141. AVStream *st = av_new_stream(s, 1);
  142. if (!st)
  143. return AVERROR(ENOMEM);
  144. av_set_pts_info(st, 32, 1, RoQ_AUDIO_SAMPLE_RATE);
  145. roq->audio_stream_index = st->index;
  146. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  147. st->codec->codec_id = CODEC_ID_ROQ_DPCM;
  148. st->codec->codec_tag = 0; /* no tag */
  149. st->codec->channels = roq->audio_channels = chunk_type == RoQ_SOUND_STEREO ? 2 : 1;
  150. st->codec->sample_rate = RoQ_AUDIO_SAMPLE_RATE;
  151. st->codec->bits_per_coded_sample = 16;
  152. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  153. st->codec->bits_per_coded_sample;
  154. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  155. }
  156. case RoQ_QUAD_VQ:
  157. /* load up the packet */
  158. if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
  159. return AVERROR(EIO);
  160. /* copy over preamble */
  161. memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
  162. if (chunk_type == RoQ_QUAD_VQ) {
  163. pkt->stream_index = roq->video_stream_index;
  164. pkt->pts = roq->video_pts++;
  165. } else {
  166. pkt->stream_index = roq->audio_stream_index;
  167. pkt->pts = roq->audio_frame_count;
  168. roq->audio_frame_count += (chunk_size / roq->audio_channels);
  169. }
  170. pkt->pos= avio_tell(pb);
  171. ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
  172. chunk_size);
  173. if (ret != chunk_size)
  174. ret = AVERROR(EIO);
  175. packet_read = 1;
  176. break;
  177. default:
  178. av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
  179. return AVERROR_INVALIDDATA;
  180. }
  181. }
  182. return ret;
  183. }
  184. AVInputFormat ff_roq_demuxer = {
  185. .name = "RoQ",
  186. .long_name = NULL_IF_CONFIG_SMALL("id RoQ format"),
  187. .priv_data_size = sizeof(RoqDemuxContext),
  188. .read_probe = roq_probe,
  189. .read_header = roq_read_header,
  190. .read_packet = roq_read_packet,
  191. };