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.

292 lines
8.7KB

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