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.

411 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;
  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. avi = av_malloc(sizeof(AVIContext));
  139. if (!avi)
  140. return -1;
  141. memset(avi, 0, sizeof(AVIContext));
  142. s->priv_data = avi;
  143. put_tag(pb, "RIFF");
  144. put_le32(pb, 0); /* file length */
  145. put_tag(pb, "AVI ");
  146. /* header list */
  147. list1 = start_tag(pb, "LIST");
  148. put_tag(pb, "hdrl");
  149. /* avi header */
  150. put_tag(pb, "avih");
  151. put_le32(pb, 14 * 4);
  152. bitrate = 0;
  153. video_enc = NULL;
  154. for(n=0;n<s->nb_streams;n++) {
  155. stream = &s->streams[n]->codec;
  156. bitrate += stream->bit_rate;
  157. if (stream->codec_type == CODEC_TYPE_VIDEO)
  158. video_enc = stream;
  159. }
  160. if (!video_enc) {
  161. av_free(avi);
  162. return -1;
  163. }
  164. nb_frames = 0;
  165. put_le32(pb, (UINT32)(INT64_C(1000000) * FRAME_RATE_BASE / video_enc->frame_rate));
  166. put_le32(pb, bitrate / 8); /* XXX: not quite exact */
  167. put_le32(pb, 0); /* padding */
  168. put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
  169. avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
  170. put_le32(pb, nb_frames); /* nb frames, filled later */
  171. put_le32(pb, 0); /* initial frame */
  172. put_le32(pb, s->nb_streams); /* nb streams */
  173. put_le32(pb, 1024 * 1024); /* suggested buffer size */
  174. put_le32(pb, video_enc->width);
  175. put_le32(pb, video_enc->height);
  176. put_le32(pb, 0); /* reserved */
  177. put_le32(pb, 0); /* reserved */
  178. put_le32(pb, 0); /* reserved */
  179. put_le32(pb, 0); /* reserved */
  180. /* stream list */
  181. for(i=0;i<n;i++) {
  182. list2 = start_tag(pb, "LIST");
  183. put_tag(pb, "strl");
  184. stream = &s->streams[i]->codec;
  185. /* stream generic header */
  186. strh = start_tag(pb, "strh");
  187. switch(stream->codec_type) {
  188. case CODEC_TYPE_VIDEO:
  189. put_tag(pb, "vids");
  190. put_le32(pb, codec_get_bmp_tag(stream->codec_id));
  191. put_le32(pb, 0); /* flags */
  192. put_le16(pb, 0); /* priority */
  193. put_le16(pb, 0); /* language */
  194. put_le32(pb, 0); /* initial frame */
  195. put_le32(pb, 1000); /* scale */
  196. put_le32(pb, (1000 * stream->frame_rate) / FRAME_RATE_BASE); /* rate */
  197. put_le32(pb, 0); /* start */
  198. avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
  199. put_le32(pb, nb_frames); /* length, XXX: fill later */
  200. put_le32(pb, 1024 * 1024); /* suggested buffer size */
  201. put_le32(pb, -1); /* quality */
  202. put_le32(pb, stream->width * stream->height * 3); /* sample size */
  203. put_le16(pb, 0);
  204. put_le16(pb, 0);
  205. put_le16(pb, stream->width);
  206. put_le16(pb, stream->height);
  207. break;
  208. case CODEC_TYPE_AUDIO:
  209. put_tag(pb, "auds");
  210. put_le32(pb, 1); /* tag */
  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. parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
  216. put_le32(pb, au_scale); /* scale */
  217. put_le32(pb, au_byterate); /* 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, 0); /* length, XXX: filled later */
  221. put_le32(pb, 12 * 1024); /* suggested buffer size */
  222. put_le32(pb, -1); /* quality */
  223. put_le32(pb, au_ssize); /* sample size */
  224. put_le32(pb, 0);
  225. put_le32(pb, 0);
  226. break;
  227. default:
  228. abort();
  229. }
  230. end_tag(pb, strh);
  231. strf = start_tag(pb, "strf");
  232. switch(stream->codec_type) {
  233. case CODEC_TYPE_VIDEO:
  234. put_bmp_header(pb, stream, codec_bmp_tags);
  235. break;
  236. case CODEC_TYPE_AUDIO:
  237. if (put_wav_header(pb, stream) < 0) {
  238. av_free(avi);
  239. return -1;
  240. }
  241. break;
  242. default:
  243. abort();
  244. }
  245. end_tag(pb, strf);
  246. end_tag(pb, list2);
  247. }
  248. end_tag(pb, list1);
  249. avi->movi_list = start_tag(pb, "LIST");
  250. avi->first = NULL;
  251. avi->last = NULL;
  252. put_tag(pb, "movi");
  253. put_flush_packet(pb);
  254. return 0;
  255. }
  256. static int avi_write_packet(AVFormatContext *s, int stream_index,
  257. UINT8 *buf, int size, int force_pts)
  258. {
  259. AVIContext *avi = s->priv_data;
  260. ByteIOContext *pb = &s->pb;
  261. AVIIndex *idx;
  262. unsigned char tag[5];
  263. unsigned int flags;
  264. AVCodecContext *enc;
  265. enc = &s->streams[stream_index]->codec;
  266. tag[0] = '0';
  267. tag[1] = '0' + stream_index;
  268. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  269. tag[2] = 'd';
  270. tag[3] = 'c';
  271. flags = enc->key_frame ? 0x10 : 0x00;
  272. } else {
  273. tag[2] = 'w';
  274. tag[3] = 'b';
  275. flags = 0x10;
  276. }
  277. if (enc->codec_type == CODEC_TYPE_AUDIO)
  278. avi->audio_strm_length[stream_index] += size;
  279. if (!url_is_streamed(&s->pb)) {
  280. idx = av_malloc(sizeof(AVIIndex));
  281. memcpy(idx->tag, tag, 4);
  282. idx->flags = flags;
  283. idx->pos = url_ftell(pb) - avi->movi_list;
  284. idx->len = size;
  285. idx->next = NULL;
  286. if (!avi->last)
  287. avi->first = idx;
  288. else
  289. avi->last->next = idx;
  290. avi->last = idx;
  291. }
  292. put_buffer(pb, tag, 4);
  293. put_le32(pb, size);
  294. put_buffer(pb, buf, size);
  295. if (size & 1)
  296. put_byte(pb, 0);
  297. put_flush_packet(pb);
  298. return 0;
  299. }
  300. static int avi_write_trailer(AVFormatContext *s)
  301. {
  302. ByteIOContext *pb = &s->pb;
  303. AVIContext *avi = s->priv_data;
  304. offset_t file_size, idx_chunk;
  305. int n, nb_frames, au_byterate, au_ssize, au_scale;
  306. AVCodecContext *stream;
  307. AVIIndex *idx;
  308. if (!url_is_streamed(&s->pb)) {
  309. end_tag(pb, avi->movi_list);
  310. idx_chunk = start_tag(pb, "idx1");
  311. idx = avi->first;
  312. while (idx != NULL) {
  313. put_buffer(pb, idx->tag, 4);
  314. put_le32(pb, idx->flags);
  315. put_le32(pb, idx->pos);
  316. put_le32(pb, idx->len);
  317. idx = idx->next;
  318. }
  319. end_tag(pb, idx_chunk);
  320. /* update file size */
  321. file_size = url_ftell(pb);
  322. url_fseek(pb, 4, SEEK_SET);
  323. put_le32(pb, (UINT32)(file_size - 8));
  324. /* Fill in frame/sample counters */
  325. nb_frames = 0;
  326. for(n=0;n<s->nb_streams;n++) {
  327. if (avi->frames_hdr_strm[n] != 0) {
  328. stream = &s->streams[n]->codec;
  329. url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
  330. if (stream->codec_type == CODEC_TYPE_VIDEO) {
  331. put_le32(pb, stream->frame_number);
  332. if (nb_frames < stream->frame_number)
  333. nb_frames = stream->frame_number;
  334. } else {
  335. if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {
  336. put_le32(pb, stream->frame_number);
  337. nb_frames += stream->frame_number;
  338. } else {
  339. parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
  340. put_le32(pb, avi->audio_strm_length[n] / au_ssize);
  341. }
  342. }
  343. }
  344. }
  345. if (avi->frames_hdr_all != 0) {
  346. url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
  347. put_le32(pb, nb_frames);
  348. }
  349. url_fseek(pb, file_size, SEEK_SET);
  350. }
  351. put_flush_packet(pb);
  352. av_free(avi);
  353. return 0;
  354. }
  355. AVFormat avi_format = {
  356. "avi",
  357. "avi format",
  358. "video/x-msvideo",
  359. "avi",
  360. CODEC_ID_MP2,
  361. CODEC_ID_MSMPEG4,
  362. avi_write_header,
  363. avi_write_packet,
  364. avi_write_trailer,
  365. avi_read_header,
  366. avi_read_packet,
  367. avi_read_close,
  368. };