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.

300 lines
8.9KB

  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 ((LE_16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
  55. (LE_32(&p->buf[2]) != 0xFFFFFFFF))
  56. return 0;
  57. return AVPROBE_SCORE_MAX;
  58. }
  59. static int roq_read_header(AVFormatContext *s,
  60. AVFormatParameters *ap)
  61. {
  62. RoqDemuxContext *roq = s->priv_data;
  63. ByteIOContext *pb = &s->pb;
  64. AVStream *st;
  65. unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
  66. int i;
  67. unsigned int chunk_size;
  68. unsigned int chunk_type;
  69. /* get the main header */
  70. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  71. RoQ_CHUNK_PREAMBLE_SIZE)
  72. return AVERROR_IO;
  73. roq->framerate = LE_16(&preamble[6]);
  74. roq->frame_pts_inc = 90000 / roq->framerate;
  75. /* set the pts reference (1 pts = 1/90000) */
  76. s->pts_num = 1;
  77. s->pts_den = 90000;
  78. /* init private context parameters */
  79. roq->width = roq->height = roq->audio_channels = roq->video_pts =
  80. roq->audio_frame_count = 0;
  81. /* scan the first n chunks searching for A/V parameters */
  82. for (i = 0; i < RoQ_CHUNKS_TO_SCAN; i++) {
  83. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  84. RoQ_CHUNK_PREAMBLE_SIZE)
  85. return AVERROR_IO;
  86. chunk_type = LE_16(&preamble[0]);
  87. chunk_size = LE_32(&preamble[2]);
  88. switch (chunk_type) {
  89. case RoQ_INFO:
  90. /* fetch the width and height; reuse the preamble bytes */
  91. if (get_buffer(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
  92. RoQ_CHUNK_PREAMBLE_SIZE)
  93. return AVERROR_IO;
  94. roq->width = LE_16(&preamble[0]);
  95. roq->height = LE_16(&preamble[2]);
  96. break;
  97. case RoQ_QUAD_CODEBOOK:
  98. case RoQ_QUAD_VQ:
  99. /* ignore during this scan */
  100. url_fseek(pb, chunk_size, SEEK_CUR);
  101. break;
  102. case RoQ_SOUND_MONO:
  103. roq->audio_channels = 1;
  104. url_fseek(pb, chunk_size, SEEK_CUR);
  105. break;
  106. case RoQ_SOUND_STEREO:
  107. roq->audio_channels = 2;
  108. url_fseek(pb, chunk_size, SEEK_CUR);
  109. break;
  110. default:
  111. printf (" unknown RoQ chunk type (%04X)\n", LE_16(&preamble[0]));
  112. return AVERROR_INVALIDDATA;
  113. break;
  114. }
  115. /* if all necessary parameters have been gathered, exit early */
  116. if ((roq->width && roq->height) && roq->audio_channels)
  117. break;
  118. }
  119. /* seek back to the first chunk */
  120. url_fseek(pb, RoQ_CHUNK_PREAMBLE_SIZE, SEEK_SET);
  121. /* initialize the decoders */
  122. st = av_new_stream(s, 0);
  123. if (!st)
  124. return AVERROR_NOMEM;
  125. roq->video_stream_index = st->index;
  126. st->codec.codec_type = CODEC_TYPE_VIDEO;
  127. st->codec.codec_id = CODEC_ID_ROQ;
  128. st->codec.codec_tag = 0; /* no fourcc */
  129. st->codec.width = roq->width;
  130. st->codec.height = roq->height;
  131. if (roq->audio_channels) {
  132. st = av_new_stream(s, 0);
  133. if (!st)
  134. return AVERROR_NOMEM;
  135. roq->audio_stream_index = st->index;
  136. st->codec.codec_type = CODEC_TYPE_AUDIO;
  137. st->codec.codec_id = CODEC_ID_ROQ_DPCM;
  138. st->codec.codec_tag = 0; /* no tag */
  139. st->codec.channels = roq->audio_channels;
  140. st->codec.sample_rate = RoQ_AUDIO_SAMPLE_RATE;
  141. st->codec.bits_per_sample = 16;
  142. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
  143. st->codec.bits_per_sample;
  144. st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
  145. }
  146. printf (" video is %d x %d, audio is %d channels\n",
  147. roq->width, roq->height, roq->audio_channels);
  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, chunk_size);
  219. if (ret != chunk_size)
  220. ret = -EIO;
  221. packet_read = 1;
  222. break;
  223. default:
  224. printf (" unknown RoQ chunk (%04X)\n", chunk_type);
  225. return AVERROR_INVALIDDATA;
  226. break;
  227. }
  228. }
  229. return ret;
  230. }
  231. static int roq_read_close(AVFormatContext *s)
  232. {
  233. // RoqDemuxContext *roq = (RoqDemuxContext *)s->priv_data;
  234. return 0;
  235. }
  236. static AVInputFormat roq_iformat = {
  237. "RoQ",
  238. "Id RoQ format",
  239. sizeof(RoqDemuxContext),
  240. roq_probe,
  241. roq_read_header,
  242. roq_read_packet,
  243. roq_read_close,
  244. };
  245. int roq_init(void)
  246. {
  247. av_register_input_format(&roq_iformat);
  248. return 0;
  249. }