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.

414 lines
12KB

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