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.

458 lines
14KB

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