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.

753 lines
24KB

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