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.

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