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.

367 lines
9.7KB

  1. /*
  2. * AVI encoder.
  3. * Copyright (c) 2000 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 "avformat.h"
  24. #include "avi.h"
  25. /*
  26. * TODO:
  27. * - fill all fields if non streamed (nb_frames for example)
  28. */
  29. typedef struct AVIIndex {
  30. unsigned char tag[4];
  31. unsigned int flags, pos, len;
  32. struct AVIIndex *next;
  33. } AVIIndex;
  34. typedef struct {
  35. offset_t movi_list;
  36. AVIIndex *first, *last;
  37. } AVIContext;
  38. offset_t start_tag(ByteIOContext *pb, char *tag)
  39. {
  40. put_tag(pb, tag);
  41. put_le32(pb, 0);
  42. return url_ftell(pb);
  43. }
  44. void end_tag(ByteIOContext *pb, offset_t start)
  45. {
  46. offset_t pos;
  47. pos = url_ftell(pb);
  48. url_fseek(pb, start - 4, SEEK_SET);
  49. put_le32(pb, pos - start);
  50. url_fseek(pb, pos, SEEK_SET);
  51. }
  52. /* Note: when encoding, the first matching tag is used, so order is
  53. important if multiple tags possible for a given codec. */
  54. CodecTag codec_bmp_tags[] = {
  55. { CODEC_ID_H263, MKTAG('U', '2', '6', '3') },
  56. { CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */
  57. { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
  58. { CODEC_ID_OPENDIVX, MKTAG('D', 'I', 'V', 'X') },
  59. { CODEC_ID_OPENDIVX, MKTAG('d', 'i', 'v', 'x') },
  60. { CODEC_ID_OPENDIVX, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
  61. { CODEC_ID_MSMPEG4, MKTAG('D', 'I', 'V', '3') }, /* default signature when using MSMPEG4 */
  62. { CODEC_ID_MSMPEG4, MKTAG('M', 'P', '4', '3') },
  63. { 0, 0 },
  64. };
  65. CodecTag codec_wav_tags[] = {
  66. { CODEC_ID_MP2, 0x55 },
  67. { CODEC_ID_MP2, 0x50 },
  68. { CODEC_ID_AC3, 0x2000 },
  69. { CODEC_ID_PCM, 0x01 },
  70. { 0, 0 },
  71. };
  72. unsigned int codec_get_tag(CodecTag *tags, int id)
  73. {
  74. while (tags->id != 0) {
  75. if (tags->id == id)
  76. return tags->tag;
  77. tags++;
  78. }
  79. return 0;
  80. }
  81. int codec_get_id(CodecTag *tags, unsigned int tag)
  82. {
  83. while (tags->id != 0) {
  84. if (tags->tag == tag)
  85. return tags->id;
  86. tags++;
  87. }
  88. return 0;
  89. }
  90. unsigned int codec_get_bmp_tag(int id)
  91. {
  92. return codec_get_tag(codec_bmp_tags, id);
  93. }
  94. /* BITMAPINFOHEADER header */
  95. void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc)
  96. {
  97. put_le32(pb, 40); /* size */
  98. put_le32(pb, enc->width);
  99. put_le32(pb, enc->height);
  100. put_le16(pb, 1); /* planes */
  101. put_le16(pb, 24); /* depth */
  102. /* compression type */
  103. put_le32(pb, codec_get_bmp_tag(enc->codec_id));
  104. put_le32(pb, enc->width * enc->height * 3);
  105. put_le32(pb, 0);
  106. put_le32(pb, 0);
  107. put_le32(pb, 0);
  108. put_le32(pb, 0);
  109. }
  110. /* WAVEFORMATEX header */
  111. void put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
  112. {
  113. int tag;
  114. tag = codec_get_tag(codec_wav_tags, enc->codec_id);
  115. put_le16(pb, tag);
  116. put_le16(pb, enc->channels);
  117. put_le32(pb, enc->sample_rate);
  118. put_le32(pb, enc->bit_rate / 8);
  119. put_le16(pb, 1); /* block align */
  120. put_le16(pb, 16); /* bits per sample */
  121. put_le16(pb, 0); /* wav_extra_size */
  122. }
  123. static int avi_write_header(AVFormatContext *s)
  124. {
  125. AVIContext *avi;
  126. ByteIOContext *pb = &s->pb;
  127. int bitrate, n, i, nb_frames;
  128. AVCodecContext *stream, *video_enc;
  129. offset_t list1, list2, strh, strf;
  130. avi = malloc(sizeof(AVIContext));
  131. if (!avi)
  132. return -1;
  133. memset(avi, 0, sizeof(AVIContext));
  134. s->priv_data = avi;
  135. put_tag(pb, "RIFF");
  136. put_le32(pb, 0); /* file length */
  137. put_tag(pb, "AVI ");
  138. /* header list */
  139. list1 = start_tag(pb, "LIST");
  140. put_tag(pb, "hdrl");
  141. /* avi header */
  142. put_tag(pb, "avih");
  143. put_le32(pb, 14 * 4);
  144. bitrate = 0;
  145. video_enc = NULL;
  146. for(n=0;n<s->nb_streams;n++) {
  147. stream = &s->streams[n]->codec;
  148. bitrate += stream->bit_rate;
  149. if (stream->codec_type == CODEC_TYPE_VIDEO)
  150. video_enc = stream;
  151. }
  152. if (!video_enc) {
  153. free(avi);
  154. return -1;
  155. }
  156. nb_frames = 0;
  157. put_le32(pb, 1000000LL * FRAME_RATE_BASE / video_enc->frame_rate);
  158. put_le32(pb, bitrate / 8); /* XXX: not quite exact */
  159. put_le32(pb, 0); /* padding */
  160. put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
  161. put_le32(pb, nb_frames); /* nb frames, filled later */
  162. put_le32(pb, 0); /* initial frame */
  163. put_le32(pb, s->nb_streams); /* nb streams */
  164. put_le32(pb, 1024 * 1024); /* suggested buffer size */
  165. put_le32(pb, video_enc->width);
  166. put_le32(pb, video_enc->height);
  167. put_le32(pb, 0); /* reserved */
  168. put_le32(pb, 0); /* reserved */
  169. put_le32(pb, 0); /* reserved */
  170. put_le32(pb, 0); /* reserved */
  171. /* stream list */
  172. for(i=0;i<n;i++) {
  173. list2 = start_tag(pb, "LIST");
  174. put_tag(pb, "strl");
  175. stream = &s->streams[i]->codec;
  176. /* stream generic header */
  177. strh = start_tag(pb, "strh");
  178. switch(stream->codec_type) {
  179. case CODEC_TYPE_VIDEO:
  180. put_tag(pb, "vids");
  181. put_le32(pb, codec_get_bmp_tag(stream->codec_id));
  182. put_le32(pb, 0); /* flags */
  183. put_le16(pb, 0); /* priority */
  184. put_le16(pb, 0); /* language */
  185. put_le32(pb, 0); /* initial frame */
  186. put_le32(pb, 1000); /* scale */
  187. put_le32(pb, (1000 * stream->frame_rate) / FRAME_RATE_BASE); /* rate */
  188. put_le32(pb, 0); /* start */
  189. put_le32(pb, nb_frames); /* length, XXX: fill later */
  190. put_le32(pb, 1024 * 1024); /* suggested buffer size */
  191. put_le32(pb, 10000); /* quality */
  192. put_le32(pb, stream->width * stream->height * 3); /* sample size */
  193. put_le16(pb, 0);
  194. put_le16(pb, 0);
  195. put_le16(pb, stream->width);
  196. put_le16(pb, stream->height);
  197. break;
  198. case CODEC_TYPE_AUDIO:
  199. put_tag(pb, "auds");
  200. put_le32(pb, 0);
  201. put_le32(pb, 0); /* flags */
  202. put_le16(pb, 0); /* priority */
  203. put_le16(pb, 0); /* language */
  204. put_le32(pb, 0); /* initial frame */
  205. put_le32(pb, 1); /* scale */
  206. put_le32(pb, stream->bit_rate / 8); /* rate */
  207. put_le32(pb, 0); /* start */
  208. put_le32(pb, 0); /* length, XXX: filled later */
  209. put_le32(pb, 12 * 1024); /* suggested buffer size */
  210. put_le32(pb, -1); /* quality */
  211. put_le32(pb, 1); /* sample size */
  212. put_le32(pb, 0);
  213. put_le32(pb, 0);
  214. break;
  215. }
  216. end_tag(pb, strh);
  217. strf = start_tag(pb, "strf");
  218. switch(stream->codec_type) {
  219. case CODEC_TYPE_VIDEO:
  220. put_bmp_header(pb, stream);
  221. break;
  222. case CODEC_TYPE_AUDIO:
  223. put_wav_header(pb, stream);
  224. break;
  225. }
  226. end_tag(pb, strf);
  227. end_tag(pb, list2);
  228. }
  229. end_tag(pb, list1);
  230. avi->movi_list = start_tag(pb, "LIST");
  231. avi->first = NULL;
  232. avi->last = NULL;
  233. put_tag(pb, "movi");
  234. put_flush_packet(pb);
  235. return 0;
  236. }
  237. static int avi_write_packet(AVFormatContext *s, int stream_index,
  238. UINT8 *buf, int size)
  239. {
  240. AVIContext *avi = s->priv_data;
  241. ByteIOContext *pb = &s->pb;
  242. AVIIndex *idx;
  243. unsigned char tag[5];
  244. unsigned int flags;
  245. AVCodecContext *enc;
  246. enc = &s->streams[stream_index]->codec;
  247. tag[0] = '0';
  248. tag[1] = '0' + stream_index;
  249. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  250. tag[2] = 'd';
  251. tag[3] = 'c';
  252. flags = enc->key_frame ? 0x10 : 0x00;
  253. } else {
  254. tag[2] = 'w';
  255. tag[3] = 'b';
  256. flags = 0x10;
  257. }
  258. if (!url_is_streamed(&s->pb)) {
  259. idx = malloc(sizeof(AVIIndex));
  260. memcpy(idx->tag, tag, 4);
  261. idx->flags = flags;
  262. idx->pos = url_ftell(pb) - avi->movi_list;
  263. idx->len = size;
  264. idx->next = NULL;
  265. if (!avi->last)
  266. avi->first = idx;
  267. else
  268. avi->last->next = idx;
  269. avi->last = idx;
  270. }
  271. put_buffer(pb, tag, 4);
  272. put_le32(pb, size);
  273. put_buffer(pb, buf, size);
  274. if (size & 1)
  275. put_byte(pb, 0);
  276. put_flush_packet(pb);
  277. return 0;
  278. }
  279. static int avi_write_trailer(AVFormatContext *s)
  280. {
  281. ByteIOContext *pb = &s->pb;
  282. AVIContext *avi = s->priv_data;
  283. offset_t file_size, idx_chunk;
  284. AVIIndex *idx;
  285. if (!url_is_streamed(&s->pb)) {
  286. end_tag(pb, avi->movi_list);
  287. idx_chunk = start_tag(pb, "idx1");
  288. idx = avi->first;
  289. while (idx != NULL) {
  290. put_buffer(pb, idx->tag, 4);
  291. put_le32(pb, idx->flags);
  292. put_le32(pb, idx->pos);
  293. put_le32(pb, idx->len);
  294. idx = idx->next;
  295. }
  296. end_tag(pb, idx_chunk);
  297. /* update file size */
  298. file_size = url_ftell(pb);
  299. url_fseek(pb, 4, SEEK_SET);
  300. put_le32(pb, file_size);
  301. url_fseek(pb, file_size, SEEK_SET);
  302. }
  303. put_flush_packet(pb);
  304. free(avi);
  305. return 0;
  306. }
  307. AVFormat avi_format = {
  308. "avi",
  309. "avi format",
  310. "video/x-msvideo",
  311. "avi",
  312. CODEC_ID_MP2,
  313. CODEC_ID_MSMPEG4,
  314. avi_write_header,
  315. avi_write_packet,
  316. avi_write_trailer,
  317. avi_read_header,
  318. avi_read_packet,
  319. avi_read_close,
  320. };