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