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.

302 lines
8.8KB

  1. /*
  2. * Id RoQ (.roq) File Demuxer
  3. * Copyright (c) 2003 The ffmpeg Project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file idroq.c
  21. * Id RoQ format file demuxer
  22. * by Mike Melanson (melanson@pcisys.net)
  23. * for more information on the .roq file format, visit:
  24. * http://www.csse.monash.edu.au/~timf/
  25. */
  26. #include "avformat.h"
  27. #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
  28. #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
  29. (((uint8_t*)(x))[2] << 16) | \
  30. (((uint8_t*)(x))[1] << 8) | \
  31. ((uint8_t*)(x))[0])
  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 width;
  43. int height;
  44. int audio_channels;
  45. int framerate;
  46. int frame_pts_inc;
  47. int video_stream_index;
  48. int audio_stream_index;
  49. int64_t video_pts;
  50. unsigned int audio_frame_count;
  51. } RoqDemuxContext;
  52. static int roq_probe(AVProbeData *p)
  53. {
  54. if (p->buf_size < 6)
  55. return 0;
  56. if ((LE_16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
  57. (LE_32(&p->buf[2]) != 0xFFFFFFFF))
  58. return 0;
  59. return AVPROBE_SCORE_MAX;
  60. }
  61. static int roq_read_header(AVFormatContext *s,
  62. AVFormatParameters *ap)
  63. {
  64. RoqDemuxContext *roq = s->priv_data;
  65. ByteIOContext *pb = &s->pb;
  66. AVStream *st;
  67. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  68. int i;
  69. unsigned int chunk_size;
  70. unsigned int chunk_type;
  71. /* get the main header */
  72. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  73. RoQ_CHUNK_PREAMBLE_SIZE)
  74. return AVERROR_IO;
  75. roq->framerate = LE_16(&preamble[6]);
  76. roq->frame_pts_inc = 90000 / roq->framerate;
  77. /* set the pts reference (1 pts = 1/90000) */
  78. s->pts_num = 1;
  79. s->pts_den = 90000;
  80. /* init private context parameters */
  81. roq->width = roq->height = roq->audio_channels = roq->video_pts =
  82. roq->audio_frame_count = 0;
  83. /* scan the first n chunks searching for A/V parameters */
  84. for (i = 0; i < RoQ_CHUNKS_TO_SCAN; i++) {
  85. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  86. RoQ_CHUNK_PREAMBLE_SIZE)
  87. return AVERROR_IO;
  88. chunk_type = LE_16(&preamble[0]);
  89. chunk_size = LE_32(&preamble[2]);
  90. switch (chunk_type) {
  91. case RoQ_INFO:
  92. /* fetch the width and height; reuse the preamble bytes */
  93. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  94. RoQ_CHUNK_PREAMBLE_SIZE)
  95. return AVERROR_IO;
  96. roq->width = LE_16(&preamble[0]);
  97. roq->height = LE_16(&preamble[2]);
  98. break;
  99. case RoQ_QUAD_CODEBOOK:
  100. case RoQ_QUAD_VQ:
  101. /* ignore during this scan */
  102. url_fseek(pb, chunk_size, SEEK_CUR);
  103. break;
  104. case RoQ_SOUND_MONO:
  105. roq->audio_channels = 1;
  106. url_fseek(pb, chunk_size, SEEK_CUR);
  107. break;
  108. case RoQ_SOUND_STEREO:
  109. roq->audio_channels = 2;
  110. url_fseek(pb, chunk_size, SEEK_CUR);
  111. break;
  112. default:
  113. printf (" unknown RoQ chunk type (%04X)\n", LE_16(&preamble[0]));
  114. return AVERROR_INVALIDDATA;
  115. break;
  116. }
  117. /* if all necessary parameters have been gathered, exit early */
  118. if ((roq->width && roq->height) && roq->audio_channels)
  119. break;
  120. }
  121. /* seek back to the first chunk */
  122. url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_SET);
  123. /* initialize the decoders */
  124. st = av_new_stream(s, 0);
  125. if (!st)
  126. return AVERROR_NOMEM;
  127. roq->video_stream_index = st->index;
  128. st->codec.codec_type = CODEC_TYPE_VIDEO;
  129. st->codec.codec_id = CODEC_ID_ROQ;
  130. st->codec.codec_tag = 0; /* no fourcc */
  131. st->codec.width = roq->width;
  132. st->codec.height = roq->height;
  133. if (roq->audio_channels) {
  134. st = av_new_stream(s, 0);
  135. if (!st)
  136. return AVERROR_NOMEM;
  137. roq->audio_stream_index = st->index;
  138. st->codec.codec_type = CODEC_TYPE_AUDIO;
  139. st->codec.codec_id = CODEC_ID_ROQ_DPCM;
  140. st->codec.codec_tag = 0; /* no tag */
  141. st->codec.channels = roq->audio_channels;
  142. st->codec.sample_rate = RoQ_AUDIO_SAMPLE_RATE;
  143. st->codec.bits_per_sample = 16;
  144. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
  145. st->codec.bits_per_sample;
  146. st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
  147. }
  148. return 0;
  149. }
  150. static int roq_read_packet(AVFormatContext *s,
  151. AVPacket *pkt)
  152. {
  153. RoqDemuxContext *roq = s->priv_data;
  154. ByteIOContext *pb = &s->pb;
  155. int ret = 0;
  156. unsigned int chunk_size;
  157. unsigned int chunk_type;
  158. unsigned int codebook_size;
  159. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  160. int packet_read = 0;
  161. offset_t codebook_offset;
  162. while (!packet_read) {
  163. if (url_feof(&s->pb))
  164. return -EIO;
  165. /* get the next chunk preamble */
  166. if ((ret = get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
  167. RoQ_CHUNK_PREAMBLE_SIZE)
  168. return -EIO;
  169. chunk_type = LE_16(&preamble[0]);
  170. chunk_size = LE_32(&preamble[2]);
  171. switch (chunk_type) {
  172. case RoQ_INFO:
  173. /* don't care about this chunk anymore */
  174. url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_CUR);
  175. break;
  176. case RoQ_QUAD_CODEBOOK:
  177. /* packet needs to contain both this codebook and next VQ chunk */
  178. codebook_offset = url_ftell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
  179. codebook_size = chunk_size;
  180. url_fseek(pb, codebook_size, SEEK_CUR);
  181. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  182. RoQ_CHUNK_PREAMBLE_SIZE)
  183. return -EIO;
  184. chunk_size = LE_32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
  185. codebook_size;
  186. /* rewind */
  187. url_fseek(pb, codebook_offset, SEEK_SET);
  188. /* load up the packet */
  189. if (av_new_packet(pkt, chunk_size))
  190. return -EIO;
  191. pkt->stream_index = roq->video_stream_index;
  192. pkt->pts = roq->video_pts;
  193. ret = get_buffer(pb, pkt->data, chunk_size);
  194. if (ret != chunk_size)
  195. ret = -EIO;
  196. roq->video_pts += roq->frame_pts_inc;
  197. packet_read = 1;
  198. break;
  199. case RoQ_SOUND_MONO:
  200. case RoQ_SOUND_STEREO:
  201. case RoQ_QUAD_VQ:
  202. /* load up the packet */
  203. if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
  204. return -EIO;
  205. /* copy over preamble */
  206. memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
  207. if (chunk_type == RoQ_QUAD_VQ) {
  208. pkt->stream_index = roq->video_stream_index;
  209. pkt->pts = roq->video_pts;
  210. roq->video_pts += roq->frame_pts_inc;
  211. } else {
  212. pkt->stream_index = roq->audio_stream_index;
  213. pkt->pts = roq->audio_frame_count;
  214. pkt->pts *= 90000;
  215. pkt->pts /= RoQ_AUDIO_SAMPLE_RATE;
  216. roq->audio_frame_count += (chunk_size / roq->audio_channels);
  217. }
  218. ret = get_buffer(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
  219. chunk_size);
  220. if (ret != chunk_size)
  221. ret = -EIO;
  222. packet_read = 1;
  223. break;
  224. default:
  225. printf (" unknown RoQ chunk (%04X)\n", chunk_type);
  226. return AVERROR_INVALIDDATA;
  227. break;
  228. }
  229. }
  230. return ret;
  231. }
  232. static int roq_read_close(AVFormatContext *s)
  233. {
  234. // RoqDemuxContext *roq = (RoqDemuxContext *)s->priv_data;
  235. return 0;
  236. }
  237. static AVInputFormat roq_iformat = {
  238. "RoQ",
  239. "Id RoQ format",
  240. sizeof(RoqDemuxContext),
  241. roq_probe,
  242. roq_read_header,
  243. roq_read_packet,
  244. roq_read_close,
  245. };
  246. int roq_init(void)
  247. {
  248. av_register_input_format(&roq_iformat);
  249. return 0;
  250. }