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.

254 lines
7.3KB

  1. /*
  2. * AVI decoder.
  3. * Copyright (c) 2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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. 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. int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
  44. {
  45. AVIContext *avi;
  46. ByteIOContext *pb = &s->pb;
  47. UINT32 tag, tag1;
  48. int codec_type, stream_index, size, frame_period, bit_rate;
  49. int i, bps;
  50. AVStream *st;
  51. avi = malloc(sizeof(AVIContext));
  52. if (!avi)
  53. return -1;
  54. memset(avi, 0, sizeof(AVIContext));
  55. s->priv_data = avi;
  56. /* check RIFF header */
  57. tag = get_le32(pb);
  58. if (tag != MKTAG('R', 'I', 'F', 'F'))
  59. return -1;
  60. get_le32(pb); /* file size */
  61. tag = get_le32(pb);
  62. if (tag != MKTAG('A', 'V', 'I', ' '))
  63. return -1;
  64. /* first list tag */
  65. stream_index = -1;
  66. codec_type = -1;
  67. frame_period = 0;
  68. for(;;) {
  69. if (url_feof(pb))
  70. goto fail;
  71. tag = get_le32(pb);
  72. size = get_le32(pb);
  73. #ifdef DEBUG
  74. print_tag("tag", tag, size);
  75. #endif
  76. switch(tag) {
  77. case MKTAG('L', 'I', 'S', 'T'):
  78. /* ignored, except when start of video packets */
  79. tag1 = get_le32(pb);
  80. #ifdef DEBUG
  81. print_tag("list", tag1, 0);
  82. #endif
  83. if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
  84. avi->movi_end = url_ftell(pb) + size - 4;
  85. #ifdef DEBUG
  86. printf("movi end=%Lx\n", avi->movi_end);
  87. #endif
  88. goto end_of_header;
  89. }
  90. break;
  91. case MKTAG('a', 'v', 'i', 'h'):
  92. /* avi header */
  93. frame_period = get_le32(pb);
  94. bit_rate = get_le32(pb) * 8;
  95. url_fskip(pb, 4 * 4);
  96. s->nb_streams = get_le32(pb);
  97. for(i=0;i<s->nb_streams;i++) {
  98. AVStream *st;
  99. st = malloc(sizeof(AVStream));
  100. if (!st)
  101. goto fail;
  102. memset(st, 0, sizeof(AVStream));
  103. s->streams[i] = st;
  104. }
  105. url_fskip(pb, size - 7 * 4);
  106. break;
  107. case MKTAG('s', 't', 'r', 'h'):
  108. /* stream header */
  109. stream_index++;
  110. tag1 = get_le32(pb);
  111. switch(tag1) {
  112. case MKTAG('v', 'i', 'd', 's'):
  113. codec_type = CODEC_TYPE_VIDEO;
  114. get_le32(pb); /* codec tag */
  115. get_le32(pb); /* flags */
  116. get_le16(pb); /* priority */
  117. get_le16(pb); /* language */
  118. get_le32(pb); /* XXX: initial frame ? */
  119. get_le32(pb); /* scale */
  120. get_le32(pb); /* rate */
  121. url_fskip(pb, size - 7 * 4);
  122. break;
  123. case MKTAG('a', 'u', 'd', 's'):
  124. codec_type = CODEC_TYPE_AUDIO;
  125. /* nothing really useful */
  126. url_fskip(pb, size - 4);
  127. break;
  128. default:
  129. goto fail;
  130. }
  131. break;
  132. case MKTAG('s', 't', 'r', 'f'):
  133. /* stream header */
  134. if (stream_index >= s->nb_streams) {
  135. url_fskip(pb, size);
  136. } else {
  137. st = s->streams[stream_index];
  138. switch(codec_type) {
  139. case CODEC_TYPE_VIDEO:
  140. get_le32(pb); /* size */
  141. st->codec.width = get_le32(pb);
  142. st->codec.height = get_le32(pb);
  143. if (frame_period)
  144. st->codec.frame_rate = (INT64_C(1000000) * FRAME_RATE_BASE) / frame_period;
  145. else
  146. st->codec.frame_rate = 25 * FRAME_RATE_BASE;
  147. get_le16(pb); /* panes */
  148. get_le16(pb); /* depth */
  149. tag1 = get_le32(pb);
  150. #ifdef DEBUG
  151. print_tag("video", tag1, 0);
  152. #endif
  153. st->codec.codec_type = CODEC_TYPE_VIDEO;
  154. st->codec.codec_tag = tag1;
  155. st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
  156. url_fskip(pb, size - 5 * 4);
  157. break;
  158. case CODEC_TYPE_AUDIO:
  159. tag1 = get_le16(pb);
  160. st->codec.codec_type = CODEC_TYPE_AUDIO;
  161. st->codec.codec_tag = tag1;
  162. #ifdef DEBUG
  163. printf("audio: 0x%x\n", tag1);
  164. #endif
  165. st->codec.channels = get_le16(pb);
  166. st->codec.sample_rate = get_le32(pb);
  167. st->codec.bit_rate = get_le32(pb) * 8;
  168. get_le16(pb); /* block align */
  169. bps = get_le16(pb);
  170. st->codec.codec_id = wav_codec_get_id(tag1, bps);
  171. url_fskip(pb, size - 4 * 4);
  172. break;
  173. default:
  174. url_fskip(pb, size);
  175. break;
  176. }
  177. }
  178. break;
  179. default:
  180. /* skip tag */
  181. size += (size & 1);
  182. url_fskip(pb, size);
  183. break;
  184. }
  185. }
  186. end_of_header:
  187. /* check stream number */
  188. if (stream_index != s->nb_streams - 1) {
  189. fail:
  190. for(i=0;i<s->nb_streams;i++) {
  191. if (s->streams[i])
  192. free(s->streams[i]);
  193. }
  194. return -1;
  195. }
  196. return 0;
  197. }
  198. int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  199. {
  200. AVIContext *avi = s->priv_data;
  201. ByteIOContext *pb = &s->pb;
  202. int n, d1, d2, size;
  203. find_next:
  204. if (url_feof(pb) || url_ftell(pb) >= avi->movi_end)
  205. return -1;
  206. d1 = get_byte(pb);
  207. if (d1 < '0' || d1 > '9')
  208. goto find_next;
  209. d2 = get_byte(pb);
  210. if (d2 < '0' || d2 > '9')
  211. goto find_next;
  212. n = (d1 - '0') * 10 + (d2 - '0');
  213. if (n < 0 || n >= s->nb_streams)
  214. goto find_next;
  215. d1 = get_byte(pb);
  216. d2 = get_byte(pb);
  217. if ((d1 != 'd' && d2 != 'c') &&
  218. (d1 != 'w' && d2 != 'b'))
  219. goto find_next;
  220. size = get_le32(pb);
  221. av_new_packet(pkt, size);
  222. pkt->stream_index = n;
  223. get_buffer(pb, pkt->data, pkt->size);
  224. if (size & 1)
  225. get_byte(pb);
  226. return 0;
  227. }
  228. int avi_read_close(AVFormatContext *s)
  229. {
  230. AVIContext *avi = s->priv_data;
  231. free(avi);
  232. return 0;
  233. }