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.

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