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.

261 lines
7.2KB

  1. /*
  2. * AVI decoder.
  3. * Copyright (c) 2001 Fabrice Bellard.
  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. #include "avformat.h"
  20. #include "avi.h"
  21. //#define DEBUG
  22. typedef struct AVIIndex {
  23. unsigned char tag[4];
  24. unsigned int flags, pos, len;
  25. struct AVIIndex *next;
  26. } AVIIndex;
  27. typedef struct {
  28. INT64 movi_end;
  29. offset_t movi_list;
  30. AVIIndex *first, *last;
  31. } AVIContext;
  32. #ifdef DEBUG
  33. static void print_tag(const char *str, unsigned int tag, int size)
  34. {
  35. printf("%s: tag=%c%c%c%c size=0x%x\n",
  36. str, tag & 0xff,
  37. (tag >> 8) & 0xff,
  38. (tag >> 16) & 0xff,
  39. (tag >> 24) & 0xff,
  40. size);
  41. }
  42. #endif
  43. static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
  44. {
  45. AVIContext *avi = s->priv_data;
  46. ByteIOContext *pb = &s->pb;
  47. UINT32 tag, tag1;
  48. int codec_type, stream_index, size, frame_period, bit_rate;
  49. int i;
  50. AVStream *st;
  51. /* check RIFF header */
  52. tag = get_le32(pb);
  53. if (tag != MKTAG('R', 'I', 'F', 'F'))
  54. return -1;
  55. get_le32(pb); /* file size */
  56. tag = get_le32(pb);
  57. if (tag != MKTAG('A', 'V', 'I', ' '))
  58. return -1;
  59. /* first list tag */
  60. stream_index = -1;
  61. codec_type = -1;
  62. frame_period = 0;
  63. for(;;) {
  64. if (url_feof(pb))
  65. goto fail;
  66. tag = get_le32(pb);
  67. size = get_le32(pb);
  68. #ifdef DEBUG
  69. print_tag("tag", tag, size);
  70. #endif
  71. switch(tag) {
  72. case MKTAG('L', 'I', 'S', 'T'):
  73. /* ignored, except when start of video packets */
  74. tag1 = get_le32(pb);
  75. #ifdef DEBUG
  76. print_tag("list", tag1, 0);
  77. #endif
  78. if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
  79. avi->movi_end = url_ftell(pb) + size - 4;
  80. #ifdef DEBUG
  81. printf("movi end=%Lx\n", avi->movi_end);
  82. #endif
  83. goto end_of_header;
  84. }
  85. break;
  86. case MKTAG('a', 'v', 'i', 'h'):
  87. /* avi header */
  88. /* using frame_period is bad idea */
  89. frame_period = get_le32(pb);
  90. bit_rate = get_le32(pb) * 8;
  91. url_fskip(pb, 4 * 4);
  92. s->nb_streams = get_le32(pb);
  93. for(i=0;i<s->nb_streams;i++) {
  94. AVStream *st = av_mallocz(sizeof(AVStream));
  95. if (!st)
  96. goto fail;
  97. avcodec_get_context_defaults(&st->codec);
  98. s->streams[i] = st;
  99. }
  100. url_fskip(pb, size - 7 * 4);
  101. break;
  102. case MKTAG('s', 't', 'r', 'h'):
  103. /* stream header */
  104. stream_index++;
  105. tag1 = get_le32(pb);
  106. switch(tag1) {
  107. case MKTAG('v', 'i', 'd', 's'):
  108. codec_type = CODEC_TYPE_VIDEO;
  109. get_le32(pb); /* codec tag */
  110. get_le32(pb); /* flags */
  111. get_le16(pb); /* priority */
  112. get_le16(pb); /* language */
  113. get_le32(pb); /* XXX: initial frame ? */
  114. get_le32(pb); /* scale */
  115. get_le32(pb); /* rate */
  116. size -= 6 * 4;
  117. break;
  118. case MKTAG('a', 'u', 'd', 's'):
  119. codec_type = CODEC_TYPE_AUDIO;
  120. /* nothing really useful */
  121. }
  122. url_fskip(pb, size - 4);
  123. break;
  124. case MKTAG('s', 't', 'r', 'f'):
  125. /* stream header */
  126. if (stream_index >= s->nb_streams) {
  127. url_fskip(pb, size);
  128. } else {
  129. st = s->streams[stream_index];
  130. switch(codec_type) {
  131. case CODEC_TYPE_VIDEO:
  132. get_le32(pb); /* size */
  133. st->codec.width = get_le32(pb);
  134. st->codec.height = get_le32(pb);
  135. if (frame_period)
  136. st->codec.frame_rate = (INT64_C(1000000) * FRAME_RATE_BASE) / frame_period;
  137. else
  138. st->codec.frame_rate = 25 * FRAME_RATE_BASE;
  139. get_le16(pb); /* panes */
  140. get_le16(pb); /* depth */
  141. tag1 = get_le32(pb);
  142. #ifdef DEBUG
  143. print_tag("video", tag1, 0);
  144. #endif
  145. st->codec.codec_type = CODEC_TYPE_VIDEO;
  146. st->codec.codec_tag = tag1;
  147. st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
  148. url_fskip(pb, size - 5 * 4);
  149. break;
  150. case CODEC_TYPE_AUDIO:
  151. get_wav_header(pb, &st->codec, (size >= 18));
  152. if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
  153. url_fskip(pb, 1);
  154. break;
  155. default:
  156. url_fskip(pb, size);
  157. break;
  158. }
  159. }
  160. break;
  161. default:
  162. /* skip tag */
  163. size += (size & 1);
  164. url_fskip(pb, size);
  165. break;
  166. }
  167. }
  168. end_of_header:
  169. /* check stream number */
  170. if (stream_index != s->nb_streams - 1) {
  171. fail:
  172. for(i=0;i<s->nb_streams;i++) {
  173. av_freep(&s->streams[i]);
  174. }
  175. return -1;
  176. }
  177. return 0;
  178. }
  179. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  180. {
  181. AVIContext *avi = s->priv_data;
  182. ByteIOContext *pb = &s->pb;
  183. int n, d1, d2, size;
  184. for(;;) {
  185. if (url_feof(pb) || url_ftell(pb) >= avi->movi_end)
  186. return -1;
  187. d1 = get_byte(pb) - '0';
  188. d2 = get_byte(pb) - '0';
  189. if (d1 < 0 || d1 > 9 || d2 < 0 || d2 > 9)
  190. continue;
  191. n = d1 * 10 + d2;
  192. if (n < 0 || n >= s->nb_streams)
  193. continue;
  194. d1 = get_byte(pb);
  195. d2 = get_byte(pb);
  196. if ((d1 == 'd' && d2 == 'c')
  197. || (d1 == 'w' && d2 == 'b'))
  198. break;
  199. }
  200. size = get_le32(pb);
  201. av_new_packet(pkt, size);
  202. pkt->stream_index = n;
  203. get_buffer(pb, pkt->data, pkt->size);
  204. if (size & 1)
  205. get_byte(pb);
  206. return 0;
  207. }
  208. static int avi_read_close(AVFormatContext *s)
  209. {
  210. return 0;
  211. }
  212. static int avi_probe(AVProbeData *p)
  213. {
  214. /* check file header */
  215. if (p->buf_size <= 32)
  216. return 0;
  217. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  218. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  219. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  220. p->buf[10] == 'I' && p->buf[11] == ' ')
  221. return AVPROBE_SCORE_MAX;
  222. else
  223. return 0;
  224. }
  225. static AVInputFormat avi_iformat = {
  226. "avi",
  227. "avi format",
  228. sizeof(AVIContext),
  229. avi_probe,
  230. avi_read_header,
  231. avi_read_packet,
  232. avi_read_close,
  233. };
  234. int avidec_init(void)
  235. {
  236. av_register_input_format(&avi_iformat);
  237. return 0;
  238. }