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.

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