idroq.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 libavformat/idroq.c
  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 framerate;
  44. int frame_pts_inc;
  45. int video_stream_index;
  46. int audio_stream_index;
  47. int64_t video_pts;
  48. unsigned int audio_frame_count;
  49. } RoqDemuxContext;
  50. static int roq_probe(AVProbeData *p)
  51. {
  52. if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
  53. (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
  54. return 0;
  55. return AVPROBE_SCORE_MAX;
  56. }
  57. static int roq_read_header(AVFormatContext *s,
  58. AVFormatParameters *ap)
  59. {
  60. RoqDemuxContext *roq = s->priv_data;
  61. ByteIOContext *pb = s->pb;
  62. AVStream *st;
  63. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  64. int i;
  65. unsigned int chunk_size;
  66. unsigned int chunk_type;
  67. /* get the main header */
  68. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  69. RoQ_CHUNK_PREAMBLE_SIZE)
  70. return AVERROR(EIO);
  71. roq->framerate = AV_RL16(&preamble[6]);
  72. roq->frame_pts_inc = 90000 / roq->framerate;
  73. /* init private context parameters */
  74. roq->width = roq->height = roq->audio_channels = roq->video_pts =
  75. roq->audio_frame_count = 0;
  76. /* scan the first n chunks searching for A/V parameters */
  77. for (i = 0; i < RoQ_CHUNKS_TO_SCAN; i++) {
  78. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  79. RoQ_CHUNK_PREAMBLE_SIZE)
  80. return AVERROR(EIO);
  81. chunk_type = AV_RL16(&preamble[0]);
  82. chunk_size = AV_RL32(&preamble[2]);
  83. switch (chunk_type) {
  84. case RoQ_INFO:
  85. /* fetch the width and height; reuse the preamble bytes */
  86. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  87. RoQ_CHUNK_PREAMBLE_SIZE)
  88. return AVERROR(EIO);
  89. roq->width = AV_RL16(&preamble[0]);
  90. roq->height = AV_RL16(&preamble[2]);
  91. break;
  92. case RoQ_QUAD_CODEBOOK:
  93. case RoQ_QUAD_VQ:
  94. /* ignore during this scan */
  95. url_fseek(pb, chunk_size, SEEK_CUR);
  96. break;
  97. case RoQ_SOUND_MONO:
  98. roq->audio_channels = 1;
  99. url_fseek(pb, chunk_size, SEEK_CUR);
  100. break;
  101. case RoQ_SOUND_STEREO:
  102. roq->audio_channels = 2;
  103. url_fseek(pb, chunk_size, SEEK_CUR);
  104. break;
  105. default:
  106. av_log(s, AV_LOG_ERROR, " unknown RoQ chunk type (%04X)\n", AV_RL16(&preamble[0]));
  107. return AVERROR_INVALIDDATA;
  108. break;
  109. }
  110. /* if all necessary parameters have been gathered, exit early */
  111. if ((roq->width && roq->height) && roq->audio_channels)
  112. break;
  113. }
  114. /* seek back to the first chunk */
  115. url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_SET);
  116. /* initialize the decoders */
  117. st = av_new_stream(s, 0);
  118. if (!st)
  119. return AVERROR(ENOMEM);
  120. /* set the pts reference (1 pts = 1/90000) */
  121. av_set_pts_info(st, 33, 1, 90000);
  122. roq->video_stream_index = st->index;
  123. st->codec->codec_type = CODEC_TYPE_VIDEO;
  124. st->codec->codec_id = CODEC_ID_ROQ;
  125. st->codec->codec_tag = 0; /* no fourcc */
  126. st->codec->width = roq->width;
  127. st->codec->height = roq->height;
  128. if (roq->audio_channels) {
  129. st = av_new_stream(s, 0);
  130. if (!st)
  131. return AVERROR(ENOMEM);
  132. av_set_pts_info(st, 33, 1, 90000);
  133. roq->audio_stream_index = st->index;
  134. st->codec->codec_type = CODEC_TYPE_AUDIO;
  135. st->codec->codec_id = CODEC_ID_ROQ_DPCM;
  136. st->codec->codec_tag = 0; /* no tag */
  137. st->codec->channels = roq->audio_channels;
  138. st->codec->sample_rate = RoQ_AUDIO_SAMPLE_RATE;
  139. st->codec->bits_per_coded_sample = 16;
  140. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  141. st->codec->bits_per_coded_sample;
  142. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  143. }
  144. return 0;
  145. }
  146. static int roq_read_packet(AVFormatContext *s,
  147. AVPacket *pkt)
  148. {
  149. RoqDemuxContext *roq = s->priv_data;
  150. ByteIOContext *pb = s->pb;
  151. int ret = 0;
  152. unsigned int chunk_size;
  153. unsigned int chunk_type;
  154. unsigned int codebook_size;
  155. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  156. int packet_read = 0;
  157. int64_t codebook_offset;
  158. while (!packet_read) {
  159. if (url_feof(s->pb))
  160. return AVERROR(EIO);
  161. /* get the next chunk preamble */
  162. if ((ret = get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
  163. RoQ_CHUNK_PREAMBLE_SIZE)
  164. return AVERROR(EIO);
  165. chunk_type = AV_RL16(&preamble[0]);
  166. chunk_size = AV_RL32(&preamble[2]);
  167. if(chunk_size > INT_MAX)
  168. return AVERROR_INVALIDDATA;
  169. switch (chunk_type) {
  170. case RoQ_INFO:
  171. /* don't care about this chunk anymore */
  172. url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_CUR);
  173. break;
  174. case RoQ_QUAD_CODEBOOK:
  175. /* packet needs to contain both this codebook and next VQ chunk */
  176. codebook_offset = url_ftell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
  177. codebook_size = chunk_size;
  178. url_fseek(pb, codebook_size, SEEK_CUR);
  179. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  180. RoQ_CHUNK_PREAMBLE_SIZE)
  181. return AVERROR(EIO);
  182. chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
  183. codebook_size;
  184. /* rewind */
  185. url_fseek(pb, codebook_offset, SEEK_SET);
  186. /* load up the packet */
  187. ret= av_get_packet(pb, pkt, chunk_size);
  188. if (ret != chunk_size)
  189. return AVERROR(EIO);
  190. pkt->stream_index = roq->video_stream_index;
  191. pkt->pts = roq->video_pts;
  192. roq->video_pts += roq->frame_pts_inc;
  193. packet_read = 1;
  194. break;
  195. case RoQ_SOUND_MONO:
  196. case RoQ_SOUND_STEREO:
  197. case RoQ_QUAD_VQ:
  198. /* load up the packet */
  199. if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
  200. return AVERROR(EIO);
  201. /* copy over preamble */
  202. memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
  203. if (chunk_type == RoQ_QUAD_VQ) {
  204. pkt->stream_index = roq->video_stream_index;
  205. pkt->pts = roq->video_pts;
  206. roq->video_pts += roq->frame_pts_inc;
  207. } else {
  208. pkt->stream_index = roq->audio_stream_index;
  209. pkt->pts = roq->audio_frame_count;
  210. pkt->pts *= 90000;
  211. pkt->pts /= RoQ_AUDIO_SAMPLE_RATE;
  212. roq->audio_frame_count += (chunk_size / roq->audio_channels);
  213. }
  214. pkt->pos= url_ftell(pb);
  215. ret = get_buffer(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
  216. chunk_size);
  217. if (ret != chunk_size)
  218. ret = AVERROR(EIO);
  219. packet_read = 1;
  220. break;
  221. default:
  222. av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
  223. return AVERROR_INVALIDDATA;
  224. break;
  225. }
  226. }
  227. return ret;
  228. }
  229. AVInputFormat roq_demuxer = {
  230. "RoQ",
  231. NULL_IF_CONFIG_SMALL("id RoQ format"),
  232. sizeof(RoqDemuxContext),
  233. roq_probe,
  234. roq_read_header,
  235. roq_read_packet,
  236. };