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.

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