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.

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