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.

422 lines
13KB

  1. /*
  2. * AVI encoder.
  3. * Copyright (c) 2000 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. /*
  22. * TODO:
  23. * - fill all fields if non streamed (nb_frames for example)
  24. */
  25. typedef struct AVIIndex {
  26. unsigned char tag[4];
  27. unsigned int flags, pos, len;
  28. struct AVIIndex *next;
  29. } AVIIndex;
  30. typedef struct {
  31. offset_t movi_list, frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
  32. int audio_strm_length[MAX_STREAMS];
  33. AVIIndex *first, *last;
  34. } AVIContext;
  35. offset_t start_tag(ByteIOContext *pb, char *tag)
  36. {
  37. put_tag(pb, tag);
  38. put_le32(pb, 0);
  39. return url_ftell(pb);
  40. }
  41. void end_tag(ByteIOContext *pb, offset_t start)
  42. {
  43. offset_t pos;
  44. pos = url_ftell(pb);
  45. url_fseek(pb, start - 4, SEEK_SET);
  46. put_le32(pb, (UINT32)(pos - start));
  47. url_fseek(pb, pos, SEEK_SET);
  48. }
  49. /* Note: when encoding, the first matching tag is used, so order is
  50. important if multiple tags possible for a given codec. */
  51. CodecTag codec_bmp_tags[] = {
  52. { CODEC_ID_H263, MKTAG('H', '2', '6', '3') },
  53. { CODEC_ID_H263P, MKTAG('H', '2', '6', '3') },
  54. { CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */
  55. { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
  56. { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') },
  57. { CODEC_ID_MPEG4, MKTAG('d', 'i', 'v', 'x') },
  58. { CODEC_ID_MPEG4, MKTAG('D', 'X', '5', '0') },
  59. { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D') },
  60. { CODEC_ID_MPEG4, MKTAG('x', 'v', 'i', 'd') },
  61. { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 's') },
  62. { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
  63. { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
  64. { CODEC_ID_MPEG4, MKTAG('m', '4', 's', '2') },
  65. { CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
  66. { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3') }, /* default signature when using MSMPEG4 */
  67. { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
  68. { CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') },
  69. { CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') },
  70. { CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') },
  71. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') },
  72. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') },
  73. { CODEC_ID_DVVIDEO, MKTAG('D', 'V', 'S', 'D') },
  74. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') },
  75. { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') },
  76. { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') },
  77. { CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') },
  78. { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
  79. { 0, 0 },
  80. };
  81. unsigned int codec_get_tag(const CodecTag *tags, int id)
  82. {
  83. while (tags->id != 0) {
  84. if (tags->id == id)
  85. return tags->tag;
  86. tags++;
  87. }
  88. return 0;
  89. }
  90. int codec_get_id(const CodecTag *tags, unsigned int tag)
  91. {
  92. while (tags->id != 0) {
  93. if (tags->tag == tag)
  94. return tags->id;
  95. tags++;
  96. }
  97. return 0;
  98. }
  99. unsigned int codec_get_bmp_tag(int id)
  100. {
  101. return codec_get_tag(codec_bmp_tags, id);
  102. }
  103. /* BITMAPINFOHEADER header */
  104. void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, CodecTag *tags)
  105. {
  106. put_le32(pb, 40); /* size */
  107. put_le32(pb, enc->width);
  108. put_le32(pb, enc->height);
  109. put_le16(pb, 1); /* planes */
  110. put_le16(pb, 24); /* depth */
  111. /* compression type */
  112. put_le32(pb, codec_get_tag(tags, enc->codec_id));
  113. put_le32(pb, enc->width * enc->height * 3);
  114. put_le32(pb, 0);
  115. put_le32(pb, 0);
  116. put_le32(pb, 0);
  117. put_le32(pb, 0);
  118. }
  119. void parse_specific_params(AVCodecContext *stream, int *au_byterate, int *au_ssize, int *au_scale)
  120. {
  121. switch(stream->codec_id) {
  122. case CODEC_ID_PCM_S16LE:
  123. *au_scale = *au_ssize = 2*stream->channels;
  124. *au_byterate = *au_ssize * stream->sample_rate;
  125. break;
  126. case CODEC_ID_PCM_U8:
  127. case CODEC_ID_PCM_ALAW:
  128. case CODEC_ID_PCM_MULAW:
  129. *au_scale = *au_ssize = stream->channels;
  130. *au_byterate = *au_ssize * stream->sample_rate;
  131. break;
  132. case CODEC_ID_MP2:
  133. *au_ssize = 1;
  134. *au_scale = 1;
  135. *au_byterate = stream->bit_rate / 8;
  136. case CODEC_ID_MP3LAME:
  137. *au_ssize = 1;
  138. *au_scale = 1;
  139. *au_byterate = stream->bit_rate / 8;
  140. default:
  141. *au_ssize = 1;
  142. *au_scale = 1;
  143. *au_byterate = stream->bit_rate / 8;
  144. break;
  145. }
  146. }
  147. static int avi_write_header(AVFormatContext *s)
  148. {
  149. AVIContext *avi = s->priv_data;
  150. ByteIOContext *pb = &s->pb;
  151. int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
  152. AVCodecContext *stream, *video_enc;
  153. offset_t list1, list2, strh, strf;
  154. put_tag(pb, "RIFF");
  155. put_le32(pb, 0); /* file length */
  156. put_tag(pb, "AVI ");
  157. /* header list */
  158. list1 = start_tag(pb, "LIST");
  159. put_tag(pb, "hdrl");
  160. /* avi header */
  161. put_tag(pb, "avih");
  162. put_le32(pb, 14 * 4);
  163. bitrate = 0;
  164. video_enc = NULL;
  165. for(n=0;n<s->nb_streams;n++) {
  166. stream = &s->streams[n]->codec;
  167. bitrate += stream->bit_rate;
  168. if (stream->codec_type == CODEC_TYPE_VIDEO)
  169. video_enc = stream;
  170. }
  171. if (!video_enc) {
  172. av_free(avi);
  173. return -1;
  174. }
  175. nb_frames = 0;
  176. put_le32(pb, (UINT32)(INT64_C(1000000) * FRAME_RATE_BASE / video_enc->frame_rate));
  177. put_le32(pb, bitrate / 8); /* XXX: not quite exact */
  178. put_le32(pb, 0); /* padding */
  179. put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
  180. avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
  181. put_le32(pb, nb_frames); /* nb frames, filled later */
  182. put_le32(pb, 0); /* initial frame */
  183. put_le32(pb, s->nb_streams); /* nb streams */
  184. put_le32(pb, 1024 * 1024); /* suggested buffer size */
  185. put_le32(pb, video_enc->width);
  186. put_le32(pb, video_enc->height);
  187. put_le32(pb, 0); /* reserved */
  188. put_le32(pb, 0); /* reserved */
  189. put_le32(pb, 0); /* reserved */
  190. put_le32(pb, 0); /* reserved */
  191. /* stream list */
  192. for(i=0;i<n;i++) {
  193. list2 = start_tag(pb, "LIST");
  194. put_tag(pb, "strl");
  195. stream = &s->streams[i]->codec;
  196. /* stream generic header */
  197. strh = start_tag(pb, "strh");
  198. switch(stream->codec_type) {
  199. case CODEC_TYPE_VIDEO:
  200. put_tag(pb, "vids");
  201. put_le32(pb, codec_get_bmp_tag(stream->codec_id));
  202. put_le32(pb, 0); /* flags */
  203. put_le16(pb, 0); /* priority */
  204. put_le16(pb, 0); /* language */
  205. put_le32(pb, 0); /* initial frame */
  206. put_le32(pb, 1000); /* scale */
  207. put_le32(pb, (1000 * stream->frame_rate) / FRAME_RATE_BASE); /* rate */
  208. put_le32(pb, 0); /* start */
  209. avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
  210. put_le32(pb, nb_frames); /* length, XXX: fill later */
  211. put_le32(pb, 1024 * 1024); /* suggested buffer size */
  212. put_le32(pb, -1); /* quality */
  213. put_le32(pb, stream->width * stream->height * 3); /* sample size */
  214. put_le16(pb, 0);
  215. put_le16(pb, 0);
  216. put_le16(pb, stream->width);
  217. put_le16(pb, stream->height);
  218. break;
  219. case CODEC_TYPE_AUDIO:
  220. put_tag(pb, "auds");
  221. put_le32(pb, 1); /* tag */
  222. put_le32(pb, 0); /* flags */
  223. put_le16(pb, 0); /* priority */
  224. put_le16(pb, 0); /* language */
  225. put_le32(pb, 0); /* initial frame */
  226. parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
  227. put_le32(pb, au_scale); /* scale */
  228. put_le32(pb, au_byterate); /* rate */
  229. put_le32(pb, 0); /* start */
  230. avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
  231. put_le32(pb, 0); /* length, XXX: filled later */
  232. put_le32(pb, 12 * 1024); /* suggested buffer size */
  233. put_le32(pb, -1); /* quality */
  234. put_le32(pb, au_ssize); /* sample size */
  235. put_le32(pb, 0);
  236. put_le32(pb, 0);
  237. break;
  238. default:
  239. av_abort();
  240. }
  241. end_tag(pb, strh);
  242. strf = start_tag(pb, "strf");
  243. switch(stream->codec_type) {
  244. case CODEC_TYPE_VIDEO:
  245. put_bmp_header(pb, stream, codec_bmp_tags);
  246. break;
  247. case CODEC_TYPE_AUDIO:
  248. if (put_wav_header(pb, stream) < 0) {
  249. av_free(avi);
  250. return -1;
  251. }
  252. break;
  253. default:
  254. av_abort();
  255. }
  256. end_tag(pb, strf);
  257. end_tag(pb, list2);
  258. }
  259. end_tag(pb, list1);
  260. avi->movi_list = start_tag(pb, "LIST");
  261. avi->first = NULL;
  262. avi->last = NULL;
  263. put_tag(pb, "movi");
  264. put_flush_packet(pb);
  265. return 0;
  266. }
  267. static int avi_write_packet(AVFormatContext *s, int stream_index,
  268. UINT8 *buf, int size, int force_pts)
  269. {
  270. AVIContext *avi = s->priv_data;
  271. ByteIOContext *pb = &s->pb;
  272. AVIIndex *idx;
  273. unsigned char tag[5];
  274. unsigned int flags;
  275. AVCodecContext *enc;
  276. enc = &s->streams[stream_index]->codec;
  277. tag[0] = '0';
  278. tag[1] = '0' + stream_index;
  279. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  280. tag[2] = 'd';
  281. tag[3] = 'c';
  282. flags = enc->key_frame ? 0x10 : 0x00;
  283. } else {
  284. tag[2] = 'w';
  285. tag[3] = 'b';
  286. flags = 0x10;
  287. }
  288. if (enc->codec_type == CODEC_TYPE_AUDIO)
  289. avi->audio_strm_length[stream_index] += size;
  290. if (!url_is_streamed(&s->pb)) {
  291. idx = av_malloc(sizeof(AVIIndex));
  292. memcpy(idx->tag, tag, 4);
  293. idx->flags = flags;
  294. idx->pos = url_ftell(pb) - avi->movi_list;
  295. idx->len = size;
  296. idx->next = NULL;
  297. if (!avi->last)
  298. avi->first = idx;
  299. else
  300. avi->last->next = idx;
  301. avi->last = idx;
  302. }
  303. put_buffer(pb, tag, 4);
  304. put_le32(pb, size);
  305. put_buffer(pb, buf, size);
  306. if (size & 1)
  307. put_byte(pb, 0);
  308. put_flush_packet(pb);
  309. return 0;
  310. }
  311. static int avi_write_trailer(AVFormatContext *s)
  312. {
  313. ByteIOContext *pb = &s->pb;
  314. AVIContext *avi = s->priv_data;
  315. offset_t file_size, idx_chunk;
  316. int n, nb_frames, au_byterate, au_ssize, au_scale;
  317. AVCodecContext *stream;
  318. AVIIndex *idx;
  319. if (!url_is_streamed(&s->pb)) {
  320. end_tag(pb, avi->movi_list);
  321. idx_chunk = start_tag(pb, "idx1");
  322. idx = avi->first;
  323. while (idx != NULL) {
  324. put_buffer(pb, idx->tag, 4);
  325. put_le32(pb, idx->flags);
  326. put_le32(pb, idx->pos);
  327. put_le32(pb, idx->len);
  328. idx = idx->next;
  329. }
  330. end_tag(pb, idx_chunk);
  331. /* update file size */
  332. file_size = url_ftell(pb);
  333. url_fseek(pb, 4, SEEK_SET);
  334. put_le32(pb, (UINT32)(file_size - 8));
  335. /* Fill in frame/sample counters */
  336. nb_frames = 0;
  337. for(n=0;n<s->nb_streams;n++) {
  338. if (avi->frames_hdr_strm[n] != 0) {
  339. stream = &s->streams[n]->codec;
  340. url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
  341. if (stream->codec_type == CODEC_TYPE_VIDEO) {
  342. put_le32(pb, stream->frame_number);
  343. if (nb_frames < stream->frame_number)
  344. nb_frames = stream->frame_number;
  345. } else {
  346. if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {
  347. put_le32(pb, stream->frame_number);
  348. nb_frames += stream->frame_number;
  349. } else {
  350. parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
  351. put_le32(pb, avi->audio_strm_length[n] / au_ssize);
  352. }
  353. }
  354. }
  355. }
  356. if (avi->frames_hdr_all != 0) {
  357. url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
  358. put_le32(pb, nb_frames);
  359. }
  360. url_fseek(pb, file_size, SEEK_SET);
  361. }
  362. put_flush_packet(pb);
  363. return 0;
  364. }
  365. static AVOutputFormat avi_oformat = {
  366. "avi",
  367. "avi format",
  368. "video/x-msvideo",
  369. "avi",
  370. sizeof(AVIContext),
  371. CODEC_ID_MP2,
  372. CODEC_ID_MSMPEG4,
  373. avi_write_header,
  374. avi_write_packet,
  375. avi_write_trailer,
  376. };
  377. int avienc_init(void)
  378. {
  379. av_register_output_format(&avi_oformat);
  380. return 0;
  381. }