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.

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