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.

260 lines
7.0KB

  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. int framenum;
  32. } AVIContext;
  33. #ifdef DEBUG
  34. static void print_tag(const char *str, unsigned int tag, int size)
  35. {
  36. printf("%s: tag=%c%c%c%c size=0x%x\n",
  37. str, tag & 0xff,
  38. (tag >> 8) & 0xff,
  39. (tag >> 16) & 0xff,
  40. (tag >> 24) & 0xff,
  41. size);
  42. }
  43. #endif
  44. static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
  45. {
  46. AVIContext *avi = s->priv_data;
  47. ByteIOContext *pb = &s->pb;
  48. UINT32 tag, tag1;
  49. int codec_type, stream_index, size, frame_period, bit_rate;
  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. avi->framenum = 0;
  65. for(;;) {
  66. if (url_feof(pb))
  67. goto fail;
  68. tag = get_le32(pb);
  69. size = get_le32(pb);
  70. #ifdef DEBUG
  71. print_tag("tag", tag, size);
  72. #endif
  73. switch(tag) {
  74. case MKTAG('L', 'I', 'S', 'T'):
  75. /* ignored, except when start of video packets */
  76. tag1 = get_le32(pb);
  77. #ifdef DEBUG
  78. print_tag("list", tag1, 0);
  79. #endif
  80. if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
  81. avi->movi_end = url_ftell(pb) + size - 4;
  82. #ifdef DEBUG
  83. printf("movi end=%Lx\n", avi->movi_end);
  84. #endif
  85. goto end_of_header;
  86. }
  87. break;
  88. case MKTAG('a', 'v', 'i', 'h'):
  89. /* avi header */
  90. /* using frame_period is bad idea */
  91. frame_period = get_le32(pb);
  92. bit_rate = get_le32(pb) * 8;
  93. url_fskip(pb, 4 * 4);
  94. s->nb_streams = get_le32(pb);
  95. for(i=0;i<s->nb_streams;i++) {
  96. AVStream *st = av_mallocz(sizeof(AVStream));
  97. if (!st)
  98. goto fail;
  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_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. break;
  154. default:
  155. url_fskip(pb, size);
  156. break;
  157. }
  158. }
  159. break;
  160. default:
  161. /* skip tag */
  162. size += (size & 1);
  163. url_fskip(pb, size);
  164. break;
  165. }
  166. }
  167. end_of_header:
  168. /* check stream number */
  169. if (stream_index != s->nb_streams - 1) {
  170. fail:
  171. for(i=0;i<s->nb_streams;i++) {
  172. av_freep(&s->streams[i]);
  173. }
  174. return -1;
  175. }
  176. return 0;
  177. }
  178. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  179. {
  180. AVIContext *avi = s->priv_data;
  181. ByteIOContext *pb = &s->pb;
  182. int n, d1, d2, size;
  183. for(;;) {
  184. if (url_feof(pb) || url_ftell(pb) >= avi->movi_end)
  185. return -1;
  186. d1 = get_byte(pb) - '0';
  187. d2 = get_byte(pb) - '0';
  188. if (d1 < 0 || d1 > 9 || d2 < 0 || d2 > 9)
  189. continue;
  190. n = d1 * 10 + d2;
  191. if (n < 0 || n >= s->nb_streams)
  192. continue;
  193. d1 = get_byte(pb);
  194. d2 = get_byte(pb);
  195. if ((d1 == 'd' && d2 == 'c')
  196. || (d1 == 'w' && d2 == 'b'))
  197. break;
  198. }
  199. size = get_le32(pb);
  200. av_new_packet(pkt, size);
  201. pkt->stream_index = n;
  202. pkt->pts = avi->framenum++;
  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. }