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.

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