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.

406 lines
12KB

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