idroqdec.c 7.5 KB

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