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.

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