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.

513 lines
16KB

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