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.

784 lines
26KB

  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. #ifdef CONFIG_MUXERS
  26. typedef struct AVIIentry {
  27. unsigned int flags, pos, len;
  28. } AVIIentry;
  29. #define AVI_INDEX_CLUSTER_SIZE 16384
  30. typedef struct AVIIndex {
  31. offset_t indx_start;
  32. int entry;
  33. int ents_allocated;
  34. AVIIentry** cluster;
  35. } AVIIndex;
  36. typedef struct {
  37. offset_t riff_start, movi_list, odml_list;
  38. offset_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
  39. int audio_strm_length[MAX_STREAMS];
  40. int riff_id;
  41. int packet_count[MAX_STREAMS];
  42. AVIIndex indexes[MAX_STREAMS];
  43. } AVIContext;
  44. static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
  45. {
  46. int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
  47. int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
  48. return &idx->cluster[cl][id];
  49. }
  50. offset_t start_tag(ByteIOContext *pb, const char *tag)
  51. {
  52. put_tag(pb, tag);
  53. put_le32(pb, 0);
  54. return url_ftell(pb);
  55. }
  56. void end_tag(ByteIOContext *pb, offset_t start)
  57. {
  58. offset_t pos;
  59. pos = url_ftell(pb);
  60. url_fseek(pb, start - 4, SEEK_SET);
  61. put_le32(pb, (uint32_t)(pos - start));
  62. url_fseek(pb, pos, SEEK_SET);
  63. }
  64. #endif //CONFIG_MUXERS
  65. /* Note: when encoding, the first matching tag is used, so order is
  66. important if multiple tags possible for a given codec. */
  67. const CodecTag codec_bmp_tags[] = {
  68. { CODEC_ID_H264, MKTAG('H', '2', '6', '4') },
  69. { CODEC_ID_H264, MKTAG('h', '2', '6', '4') },
  70. { CODEC_ID_H264, MKTAG('X', '2', '6', '4') },
  71. { CODEC_ID_H264, MKTAG('x', '2', '6', '4') },
  72. { CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') },
  73. { CODEC_ID_H264, MKTAG('V', 'S', 'S', 'H') },
  74. { CODEC_ID_H263, MKTAG('H', '2', '6', '3') },
  75. { CODEC_ID_H263P, MKTAG('H', '2', '6', '3') },
  76. { CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */
  77. { CODEC_ID_H261, MKTAG('H', '2', '6', '1') },
  78. /* added based on MPlayer */
  79. { CODEC_ID_H263P, MKTAG('U', '2', '6', '3') },
  80. { CODEC_ID_H263P, MKTAG('v', 'i', 'v', '1') },
  81. { CODEC_ID_MPEG4, MKTAG('F', 'M', 'P', '4')},
  82. { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X'), .invalid_asf = 1 },
  83. { CODEC_ID_MPEG4, MKTAG('D', 'X', '5', '0'), .invalid_asf = 1 },
  84. { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D'), .invalid_asf = 1 },
  85. { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
  86. { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
  87. { CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
  88. /* added based on MPlayer */
  89. { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', '1') },
  90. { CODEC_ID_MPEG4, MKTAG('B', 'L', 'Z', '0') },
  91. { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
  92. { CODEC_ID_MPEG4, MKTAG('U', 'M', 'P', '4') },
  93. { CODEC_ID_MPEG4, MKTAG('W', 'V', '1', 'F') },
  94. { CODEC_ID_MPEG4, MKTAG('S', 'E', 'D', 'G') },
  95. { CODEC_ID_MPEG4, MKTAG('R', 'M', 'P', '4') },
  96. { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3'), .invalid_asf = 1 }, /* default signature when using MSMPEG4 */
  97. { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
  98. /* added based on MPlayer */
  99. { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', 'G', '3') },
  100. { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '5') },
  101. { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '6') },
  102. { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '4') },
  103. { CODEC_ID_MSMPEG4V3, MKTAG('A', 'P', '4', '1') },
  104. { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '1') },
  105. { CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '0') },
  106. { CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') },
  107. /* added based on MPlayer */
  108. { CODEC_ID_MSMPEG4V2, MKTAG('D', 'I', 'V', '2') },
  109. { CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') },
  110. { CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') },
  111. /* added based on MPlayer */
  112. { CODEC_ID_WMV2, MKTAG('W', 'M', 'V', '2') },
  113. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') },
  114. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') },
  115. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') },
  116. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '2', '5') },
  117. { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') },
  118. { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') },
  119. { CODEC_ID_MPEG2VIDEO, MKTAG('m', 'p', 'g', '2') },
  120. { CODEC_ID_MPEG2VIDEO, MKTAG('M', 'P', 'E', 'G') },
  121. { CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') },
  122. { CODEC_ID_MPEG1VIDEO, MKTAG('V', 'C', 'R', '2') },
  123. { CODEC_ID_MPEG1VIDEO, 0x10000001 },
  124. { CODEC_ID_MPEG2VIDEO, 0x10000002 },
  125. { CODEC_ID_MPEG2VIDEO, MKTAG('D', 'V', 'R', ' ') },
  126. { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
  127. { CODEC_ID_MJPEG, MKTAG('L', 'J', 'P', 'G') },
  128. { CODEC_ID_LJPEG, MKTAG('L', 'J', 'P', 'G') },
  129. { CODEC_ID_MJPEG, MKTAG('J', 'P', 'G', 'L') }, /* Pegasus lossless JPEG */
  130. { CODEC_ID_MJPEG, MKTAG('M', 'J', 'L', 'S') }, /* JPEG-LS custom FOURCC for avi - decoder */
  131. { CODEC_ID_JPEGLS, MKTAG('M', 'J', 'L', 'S') }, /* JPEG-LS custom FOURCC for avi - encoder */
  132. { CODEC_ID_HUFFYUV, MKTAG('H', 'F', 'Y', 'U') },
  133. { CODEC_ID_FFVHUFF, MKTAG('F', 'F', 'V', 'H') },
  134. { CODEC_ID_CYUV, MKTAG('C', 'Y', 'U', 'V') },
  135. { CODEC_ID_RAWVIDEO, MKTAG('I', '4', '2', '0') },
  136. { CODEC_ID_RAWVIDEO, MKTAG('Y', 'U', 'Y', '2') },
  137. { CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', '2') },
  138. { CODEC_ID_RAWVIDEO, MKTAG('U', 'Y', 'V', 'Y') },
  139. { CODEC_ID_RAWVIDEO, MKTAG('I', 'Y', 'U', 'V') },
  140. { CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '1') },
  141. { CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '2') },
  142. { CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') },
  143. { CODEC_ID_VP3, MKTAG('V', 'P', '3', '0') },
  144. { CODEC_ID_ASV1, MKTAG('A', 'S', 'V', '1') },
  145. { CODEC_ID_ASV2, MKTAG('A', 'S', 'V', '2') },
  146. { CODEC_ID_VCR1, MKTAG('V', 'C', 'R', '1') },
  147. { CODEC_ID_FFV1, MKTAG('F', 'F', 'V', '1') },
  148. { CODEC_ID_XAN_WC4, MKTAG('X', 'x', 'a', 'n') },
  149. { CODEC_ID_MSRLE, MKTAG('m', 'r', 'l', 'e') },
  150. { CODEC_ID_MSRLE, MKTAG(0x1, 0x0, 0x0, 0x0) },
  151. { CODEC_ID_MSVIDEO1, MKTAG('M', 'S', 'V', 'C') },
  152. { CODEC_ID_MSVIDEO1, MKTAG('m', 's', 'v', 'c') },
  153. { CODEC_ID_MSVIDEO1, MKTAG('C', 'R', 'A', 'M') },
  154. { CODEC_ID_MSVIDEO1, MKTAG('c', 'r', 'a', 'm') },
  155. { CODEC_ID_MSVIDEO1, MKTAG('W', 'H', 'A', 'M') },
  156. { CODEC_ID_MSVIDEO1, MKTAG('w', 'h', 'a', 'm') },
  157. { CODEC_ID_CINEPAK, MKTAG('c', 'v', 'i', 'd') },
  158. { CODEC_ID_TRUEMOTION1, MKTAG('D', 'U', 'C', 'K') },
  159. { CODEC_ID_MSZH, MKTAG('M', 'S', 'Z', 'H') },
  160. { CODEC_ID_ZLIB, MKTAG('Z', 'L', 'I', 'B') },
  161. { CODEC_ID_SNOW, MKTAG('S', 'N', 'O', 'W') },
  162. { CODEC_ID_4XM, MKTAG('4', 'X', 'M', 'V') },
  163. { CODEC_ID_FLV1, MKTAG('F', 'L', 'V', '1') },
  164. { CODEC_ID_FLASHSV, MKTAG('F', 'S', 'V', '1') },
  165. { CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') },
  166. { CODEC_ID_TSCC, MKTAG('t', 's', 'c', 'c') },
  167. { CODEC_ID_ULTI, MKTAG('U', 'L', 'T', 'I') },
  168. { CODEC_ID_VIXL, MKTAG('V', 'I', 'X', 'L') },
  169. { CODEC_ID_QPEG, MKTAG('Q', 'P', 'E', 'G') },
  170. { CODEC_ID_QPEG, MKTAG('Q', '1', '.', '0') },
  171. { CODEC_ID_QPEG, MKTAG('Q', '1', '.', '1') },
  172. { CODEC_ID_WMV3, MKTAG('W', 'M', 'V', '3') },
  173. { CODEC_ID_LOCO, MKTAG('L', 'O', 'C', 'O') },
  174. { CODEC_ID_WNV1, MKTAG('W', 'N', 'V', '1') },
  175. { CODEC_ID_AASC, MKTAG('A', 'A', 'S', 'C') },
  176. { CODEC_ID_INDEO2, MKTAG('R', 'T', '2', '1') },
  177. { CODEC_ID_FRAPS, MKTAG('F', 'P', 'S', '1') },
  178. { CODEC_ID_THEORA, MKTAG('t', 'h', 'e', 'o') },
  179. { CODEC_ID_TRUEMOTION2, MKTAG('T', 'M', '2', '0') },
  180. { CODEC_ID_CSCD, MKTAG('C', 'S', 'C', 'D') },
  181. { CODEC_ID_ZMBV, MKTAG('Z', 'M', 'B', 'V') },
  182. { CODEC_ID_KMVC, MKTAG('K', 'M', 'V', 'C') },
  183. { CODEC_ID_RAWVIDEO, 0 },
  184. { CODEC_ID_NONE, 0 },
  185. };
  186. unsigned int codec_get_tag(const CodecTag *tags, int id)
  187. {
  188. while (tags->id != CODEC_ID_NONE) {
  189. if (tags->id == id)
  190. return tags->tag;
  191. tags++;
  192. }
  193. return 0;
  194. }
  195. static unsigned int codec_get_asf_tag(const CodecTag *tags, unsigned int id)
  196. {
  197. while (tags->id != CODEC_ID_NONE) {
  198. if (!tags->invalid_asf && tags->id == id)
  199. return tags->tag;
  200. tags++;
  201. }
  202. return 0;
  203. }
  204. enum CodecID codec_get_id(const CodecTag *tags, unsigned int tag)
  205. {
  206. while (tags->id != CODEC_ID_NONE) {
  207. if( toupper((tag >> 0)&0xFF) == toupper((tags->tag >> 0)&0xFF)
  208. && toupper((tag >> 8)&0xFF) == toupper((tags->tag >> 8)&0xFF)
  209. && toupper((tag >>16)&0xFF) == toupper((tags->tag >>16)&0xFF)
  210. && toupper((tag >>24)&0xFF) == toupper((tags->tag >>24)&0xFF))
  211. return tags->id;
  212. tags++;
  213. }
  214. return CODEC_ID_NONE;
  215. }
  216. unsigned int codec_get_bmp_tag(int id)
  217. {
  218. return codec_get_tag(codec_bmp_tags, id);
  219. }
  220. unsigned int codec_get_wav_tag(int id)
  221. {
  222. return codec_get_tag(codec_wav_tags, id);
  223. }
  224. enum CodecID codec_get_bmp_id(unsigned int tag)
  225. {
  226. return codec_get_id(codec_bmp_tags, tag);
  227. }
  228. enum CodecID codec_get_wav_id(unsigned int tag)
  229. {
  230. return codec_get_id(codec_wav_tags, tag);
  231. }
  232. #ifdef CONFIG_MUXERS
  233. /* BITMAPINFOHEADER header */
  234. void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, const CodecTag *tags, int for_asf)
  235. {
  236. put_le32(pb, 40 + enc->extradata_size); /* size */
  237. put_le32(pb, enc->width);
  238. put_le32(pb, enc->height);
  239. put_le16(pb, 1); /* planes */
  240. put_le16(pb, enc->bits_per_sample ? enc->bits_per_sample : 24); /* depth */
  241. /* compression type */
  242. put_le32(pb, for_asf ? (enc->codec_tag ? enc->codec_tag : codec_get_asf_tag(tags, enc->codec_id)) : enc->codec_tag); //
  243. put_le32(pb, enc->width * enc->height * 3);
  244. put_le32(pb, 0);
  245. put_le32(pb, 0);
  246. put_le32(pb, 0);
  247. put_le32(pb, 0);
  248. put_buffer(pb, enc->extradata, enc->extradata_size);
  249. if (enc->extradata_size & 1)
  250. put_byte(pb, 0);
  251. }
  252. void ff_parse_specific_params(AVCodecContext *stream, int *au_rate, int *au_ssize, int *au_scale)
  253. {
  254. int gcd;
  255. *au_ssize= stream->block_align;
  256. if(stream->frame_size && stream->sample_rate){
  257. *au_scale=stream->frame_size;
  258. *au_rate= stream->sample_rate;
  259. }else if(stream->codec_type == CODEC_TYPE_VIDEO){
  260. *au_scale= stream->time_base.num;
  261. *au_rate = stream->time_base.den;
  262. }else{
  263. *au_scale= stream->block_align ? stream->block_align*8 : 8;
  264. *au_rate = stream->bit_rate;
  265. }
  266. gcd= ff_gcd(*au_scale, *au_rate);
  267. *au_scale /= gcd;
  268. *au_rate /= gcd;
  269. }
  270. static offset_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb,
  271. const char* riff_tag, const char* list_tag)
  272. {
  273. offset_t loff;
  274. int i;
  275. avi->riff_id++;
  276. for (i=0; i<MAX_STREAMS; i++)
  277. avi->indexes[i].entry = 0;
  278. avi->riff_start = start_tag(pb, "RIFF");
  279. put_tag(pb, riff_tag);
  280. loff = start_tag(pb, "LIST");
  281. put_tag(pb, list_tag);
  282. return loff;
  283. }
  284. static unsigned char* avi_stream2fourcc(unsigned char* tag, int index,
  285. enum CodecType type)
  286. {
  287. tag[0] = '0';
  288. tag[1] = '0' + index;
  289. if (type == CODEC_TYPE_VIDEO) {
  290. tag[2] = 'd';
  291. tag[3] = 'c';
  292. } else {
  293. tag[2] = 'w';
  294. tag[3] = 'b';
  295. }
  296. tag[4] = '\0';
  297. return tag;
  298. }
  299. static int avi_write_header(AVFormatContext *s)
  300. {
  301. AVIContext *avi = s->priv_data;
  302. ByteIOContext *pb = &s->pb;
  303. int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
  304. AVCodecContext *stream, *video_enc;
  305. offset_t list1, list2, strh, strf;
  306. /* header list */
  307. avi->riff_id = 0;
  308. list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl");
  309. /* avi header */
  310. put_tag(pb, "avih");
  311. put_le32(pb, 14 * 4);
  312. bitrate = 0;
  313. video_enc = NULL;
  314. for(n=0;n<s->nb_streams;n++) {
  315. stream = s->streams[n]->codec;
  316. bitrate += stream->bit_rate;
  317. if (stream->codec_type == CODEC_TYPE_VIDEO)
  318. video_enc = stream;
  319. }
  320. nb_frames = 0;
  321. if(video_enc){
  322. put_le32(pb, (uint32_t)(int64_t_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
  323. } else {
  324. put_le32(pb, 0);
  325. }
  326. put_le32(pb, bitrate / 8); /* XXX: not quite exact */
  327. put_le32(pb, 0); /* padding */
  328. if (url_is_streamed(pb))
  329. put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
  330. else
  331. put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
  332. avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
  333. put_le32(pb, nb_frames); /* nb frames, filled later */
  334. put_le32(pb, 0); /* initial frame */
  335. put_le32(pb, s->nb_streams); /* nb streams */
  336. put_le32(pb, 1024 * 1024); /* suggested buffer size */
  337. if(video_enc){
  338. put_le32(pb, video_enc->width);
  339. put_le32(pb, video_enc->height);
  340. } else {
  341. put_le32(pb, 0);
  342. put_le32(pb, 0);
  343. }
  344. put_le32(pb, 0); /* reserved */
  345. put_le32(pb, 0); /* reserved */
  346. put_le32(pb, 0); /* reserved */
  347. put_le32(pb, 0); /* reserved */
  348. /* stream list */
  349. for(i=0;i<n;i++) {
  350. list2 = start_tag(pb, "LIST");
  351. put_tag(pb, "strl");
  352. stream = s->streams[i]->codec;
  353. /* FourCC should really be set by the codec itself */
  354. if (! stream->codec_tag) {
  355. stream->codec_tag = codec_get_bmp_tag(stream->codec_id);
  356. }
  357. /* stream generic header */
  358. strh = start_tag(pb, "strh");
  359. switch(stream->codec_type) {
  360. case CODEC_TYPE_VIDEO: put_tag(pb, "vids"); break;
  361. case CODEC_TYPE_AUDIO: put_tag(pb, "auds"); break;
  362. // case CODEC_TYPE_TEXT : put_tag(pb, "txts"); break;
  363. case CODEC_TYPE_DATA : put_tag(pb, "dats"); break;
  364. }
  365. if(stream->codec_type == CODEC_TYPE_VIDEO)
  366. put_le32(pb, stream->codec_tag);
  367. else
  368. put_le32(pb, 1);
  369. put_le32(pb, 0); /* flags */
  370. put_le16(pb, 0); /* priority */
  371. put_le16(pb, 0); /* language */
  372. put_le32(pb, 0); /* initial frame */
  373. ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
  374. put_le32(pb, au_scale); /* scale */
  375. put_le32(pb, au_byterate); /* rate */
  376. av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
  377. put_le32(pb, 0); /* start */
  378. avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
  379. if (url_is_streamed(pb))
  380. put_le32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
  381. else
  382. put_le32(pb, 0); /* length, XXX: filled later */
  383. /* suggested buffer size */ //FIXME set at the end to largest chunk
  384. if(stream->codec_type == CODEC_TYPE_VIDEO)
  385. put_le32(pb, 1024 * 1024);
  386. else if(stream->codec_type == CODEC_TYPE_AUDIO)
  387. put_le32(pb, 12 * 1024);
  388. else
  389. put_le32(pb, 0);
  390. put_le32(pb, -1); /* quality */
  391. put_le32(pb, au_ssize); /* sample size */
  392. put_le32(pb, 0);
  393. put_le16(pb, stream->width);
  394. put_le16(pb, stream->height);
  395. end_tag(pb, strh);
  396. if(stream->codec_type != CODEC_TYPE_DATA){
  397. strf = start_tag(pb, "strf");
  398. switch(stream->codec_type) {
  399. case CODEC_TYPE_VIDEO:
  400. put_bmp_header(pb, stream, codec_bmp_tags, 0);
  401. break;
  402. case CODEC_TYPE_AUDIO:
  403. if (put_wav_header(pb, stream) < 0) {
  404. av_free(avi);
  405. return -1;
  406. }
  407. break;
  408. default:
  409. return -1;
  410. }
  411. end_tag(pb, strf);
  412. }
  413. if (!url_is_streamed(pb)) {
  414. unsigned char tag[5];
  415. int j;
  416. /* Starting to lay out AVI OpenDML master index.
  417. * We want to make it JUNK entry for now, since we'd
  418. * like to get away without making AVI an OpenDML one
  419. * for compatibility reasons.
  420. */
  421. avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0;
  422. avi->indexes[i].indx_start = start_tag(pb, "JUNK");
  423. put_le16(pb, 4); /* wLongsPerEntry */
  424. put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
  425. put_byte(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
  426. put_le32(pb, 0); /* nEntriesInUse (will fill out later on) */
  427. put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
  428. /* dwChunkId */
  429. put_le64(pb, 0); /* dwReserved[3]
  430. put_le32(pb, 0); Must be 0. */
  431. for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
  432. put_le64(pb, 0);
  433. end_tag(pb, avi->indexes[i].indx_start);
  434. }
  435. end_tag(pb, list2);
  436. }
  437. if (!url_is_streamed(pb)) {
  438. /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
  439. avi->odml_list = start_tag(pb, "JUNK");
  440. put_tag(pb, "odml");
  441. put_tag(pb, "dmlh");
  442. put_le32(pb, 248);
  443. for (i = 0; i < 248; i+= 4)
  444. put_le32(pb, 0);
  445. end_tag(pb, avi->odml_list);
  446. }
  447. end_tag(pb, list1);
  448. avi->movi_list = start_tag(pb, "LIST");
  449. put_tag(pb, "movi");
  450. put_flush_packet(pb);
  451. return 0;
  452. }
  453. static int avi_write_ix(AVFormatContext *s)
  454. {
  455. ByteIOContext *pb = &s->pb;
  456. AVIContext *avi = s->priv_data;
  457. unsigned char tag[5];
  458. unsigned char ix_tag[] = "ix00";
  459. int i, j;
  460. assert(!url_is_streamed(pb));
  461. if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
  462. return -1;
  463. for (i=0;i<s->nb_streams;i++) {
  464. offset_t ix, pos;
  465. avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
  466. ix_tag[3] = '0' + i;
  467. /* Writing AVI OpenDML leaf index chunk */
  468. ix = url_ftell(pb);
  469. put_tag(pb, &ix_tag[0]); /* ix?? */
  470. put_le32(pb, avi->indexes[i].entry * 8 + 24);
  471. /* chunk size */
  472. put_le16(pb, 2); /* wLongsPerEntry */
  473. put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
  474. put_byte(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
  475. put_le32(pb, avi->indexes[i].entry);
  476. /* nEntriesInUse */
  477. put_tag(pb, &tag[0]); /* dwChunkId */
  478. put_le64(pb, avi->movi_list);/* qwBaseOffset */
  479. put_le32(pb, 0); /* dwReserved_3 (must be 0) */
  480. for (j=0; j<avi->indexes[i].entry; j++) {
  481. AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j);
  482. put_le32(pb, ie->pos + 8);
  483. put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
  484. (ie->flags & 0x10 ? 0 : 0x80000000));
  485. }
  486. put_flush_packet(pb);
  487. pos = url_ftell(pb);
  488. /* Updating one entry in the AVI OpenDML master index */
  489. url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET);
  490. put_tag(pb, "indx"); /* enabling this entry */
  491. url_fskip(pb, 8);
  492. put_le32(pb, avi->riff_id); /* nEntriesInUse */
  493. url_fskip(pb, 16*avi->riff_id);
  494. put_le64(pb, ix); /* qwOffset */
  495. put_le32(pb, pos - ix); /* dwSize */
  496. put_le32(pb, avi->indexes[i].entry); /* dwDuration */
  497. url_fseek(pb, pos, SEEK_SET);
  498. }
  499. return 0;
  500. }
  501. static int avi_write_idx1(AVFormatContext *s)
  502. {
  503. ByteIOContext *pb = &s->pb;
  504. AVIContext *avi = s->priv_data;
  505. offset_t file_size, idx_chunk;
  506. int i, n, nb_frames, au_byterate, au_ssize, au_scale;
  507. AVCodecContext *stream;
  508. unsigned char tag[5];
  509. if (!url_is_streamed(pb)) {
  510. AVIIentry* ie = 0, *tie;
  511. int entry[MAX_STREAMS];
  512. int empty, stream_id = -1;
  513. idx_chunk = start_tag(pb, "idx1");
  514. memset(&entry[0], 0, sizeof(entry));
  515. do {
  516. empty = 1;
  517. for (i=0; i<s->nb_streams; i++) {
  518. if (avi->indexes[i].entry <= entry[i])
  519. continue;
  520. tie = avi_get_ientry(&avi->indexes[i], entry[i]);
  521. if (empty || tie->pos < ie->pos) {
  522. ie = tie;
  523. stream_id = i;
  524. }
  525. empty = 0;
  526. }
  527. if (!empty) {
  528. avi_stream2fourcc(&tag[0], stream_id,
  529. s->streams[stream_id]->codec->codec_type);
  530. put_tag(pb, &tag[0]);
  531. put_le32(pb, ie->flags);
  532. put_le32(pb, ie->pos);
  533. put_le32(pb, ie->len);
  534. entry[stream_id]++;
  535. }
  536. } while (!empty);
  537. end_tag(pb, idx_chunk);
  538. /* Fill in frame/sample counters */
  539. file_size = url_ftell(pb);
  540. nb_frames = 0;
  541. for(n=0;n<s->nb_streams;n++) {
  542. assert(avi->frames_hdr_strm[n]);
  543. stream = s->streams[n]->codec;
  544. url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
  545. ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
  546. if (au_ssize == 0) {
  547. put_le32(pb, avi->packet_count[n]);
  548. } else {
  549. put_le32(pb, avi->audio_strm_length[n] / au_ssize);
  550. }
  551. if(stream->codec_type == CODEC_TYPE_VIDEO)
  552. nb_frames = FFMAX(nb_frames, avi->packet_count[n]);
  553. }
  554. assert(avi->frames_hdr_all);
  555. url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
  556. put_le32(pb, nb_frames);
  557. url_fseek(pb, file_size, SEEK_SET);
  558. }
  559. return 0;
  560. }
  561. static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
  562. {
  563. AVIContext *avi = s->priv_data;
  564. ByteIOContext *pb = &s->pb;
  565. unsigned char tag[5];
  566. unsigned int flags=0;
  567. const int stream_index= pkt->stream_index;
  568. AVCodecContext *enc= s->streams[stream_index]->codec;
  569. int size= pkt->size;
  570. // av_log(s, AV_LOG_DEBUG, "%lld %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
  571. while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avi->packet_count[stream_index]){
  572. AVPacket empty_packet;
  573. av_init_packet(&empty_packet);
  574. empty_packet.size= 0;
  575. empty_packet.data= NULL;
  576. empty_packet.stream_index= stream_index;
  577. avi_write_packet(s, &empty_packet);
  578. // av_log(s, AV_LOG_DEBUG, "dup %lld %d\n", pkt->dts, avi->packet_count[stream_index]);
  579. }
  580. avi->packet_count[stream_index]++;
  581. // Make sure to put an OpenDML chunk when the file size exceeds the limits
  582. if (!url_is_streamed(pb) &&
  583. (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
  584. avi_write_ix(s);
  585. end_tag(pb, avi->movi_list);
  586. if (avi->riff_id == 1)
  587. avi_write_idx1(s);
  588. end_tag(pb, avi->riff_start);
  589. avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi");
  590. }
  591. avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
  592. if(pkt->flags&PKT_FLAG_KEY)
  593. flags = 0x10;
  594. if (enc->codec_type == CODEC_TYPE_AUDIO) {
  595. avi->audio_strm_length[stream_index] += size;
  596. }
  597. if (!url_is_streamed(&s->pb)) {
  598. AVIIndex* idx = &avi->indexes[stream_index];
  599. int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
  600. int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
  601. if (idx->ents_allocated <= idx->entry) {
  602. idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
  603. if (!idx->cluster)
  604. return -1;
  605. idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
  606. if (!idx->cluster[cl])
  607. return -1;
  608. idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
  609. }
  610. idx->cluster[cl][id].flags = flags;
  611. idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
  612. idx->cluster[cl][id].len = size;
  613. idx->entry++;
  614. }
  615. put_buffer(pb, tag, 4);
  616. put_le32(pb, size);
  617. put_buffer(pb, pkt->data, size);
  618. if (size & 1)
  619. put_byte(pb, 0);
  620. put_flush_packet(pb);
  621. return 0;
  622. }
  623. static int avi_write_trailer(AVFormatContext *s)
  624. {
  625. AVIContext *avi = s->priv_data;
  626. ByteIOContext *pb = &s->pb;
  627. int res = 0;
  628. int i, j, n, nb_frames;
  629. offset_t file_size;
  630. if (!url_is_streamed(pb))
  631. {
  632. if (avi->riff_id == 1) {
  633. end_tag(pb, avi->movi_list);
  634. res = avi_write_idx1(s);
  635. end_tag(pb, avi->riff_start);
  636. } else {
  637. avi_write_ix(s);
  638. end_tag(pb, avi->movi_list);
  639. end_tag(pb, avi->riff_start);
  640. file_size = url_ftell(pb);
  641. url_fseek(pb, avi->odml_list - 8, SEEK_SET);
  642. put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
  643. url_fskip(pb, 16);
  644. for (n=nb_frames=0;n<s->nb_streams;n++) {
  645. AVCodecContext *stream = s->streams[n]->codec;
  646. if (stream->codec_type == CODEC_TYPE_VIDEO) {
  647. if (nb_frames < avi->packet_count[n])
  648. nb_frames = avi->packet_count[n];
  649. } else {
  650. if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
  651. nb_frames += avi->packet_count[n];
  652. }
  653. }
  654. }
  655. put_le32(pb, nb_frames);
  656. url_fseek(pb, file_size, SEEK_SET);
  657. }
  658. }
  659. put_flush_packet(pb);
  660. for (i=0; i<MAX_STREAMS; i++) {
  661. for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
  662. av_free(avi->indexes[i].cluster[j]);
  663. av_free(avi->indexes[i].cluster);
  664. avi->indexes[i].cluster = NULL;
  665. avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0;
  666. }
  667. return res;
  668. }
  669. static AVOutputFormat avi_oformat = {
  670. "avi",
  671. "avi format",
  672. "video/x-msvideo",
  673. "avi",
  674. sizeof(AVIContext),
  675. CODEC_ID_MP2,
  676. CODEC_ID_MPEG4,
  677. avi_write_header,
  678. avi_write_packet,
  679. avi_write_trailer,
  680. };
  681. int avienc_init(void)
  682. {
  683. av_register_output_format(&avi_oformat);
  684. return 0;
  685. }
  686. #endif //CONFIG_MUXERS