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.

561 lines
18KB

  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. #include "riff.h"
  22. /*
  23. * TODO:
  24. * - fill all fields if non streamed (nb_frames for example)
  25. */
  26. #ifdef CONFIG_AVI_MUXER
  27. typedef struct AVIIentry {
  28. unsigned int flags, pos, len;
  29. } AVIIentry;
  30. #define AVI_INDEX_CLUSTER_SIZE 16384
  31. typedef struct AVIIndex {
  32. offset_t indx_start;
  33. int entry;
  34. int ents_allocated;
  35. AVIIentry** cluster;
  36. } AVIIndex;
  37. typedef struct {
  38. offset_t riff_start, movi_list, odml_list;
  39. offset_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
  40. int audio_strm_length[MAX_STREAMS];
  41. int riff_id;
  42. int packet_count[MAX_STREAMS];
  43. AVIIndex indexes[MAX_STREAMS];
  44. } AVIContext;
  45. static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
  46. {
  47. int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
  48. int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
  49. return &idx->cluster[cl][id];
  50. }
  51. static offset_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb,
  52. const char* riff_tag, const char* list_tag)
  53. {
  54. offset_t loff;
  55. int i;
  56. avi->riff_id++;
  57. for (i=0; i<MAX_STREAMS; i++)
  58. avi->indexes[i].entry = 0;
  59. avi->riff_start = start_tag(pb, "RIFF");
  60. put_tag(pb, riff_tag);
  61. loff = start_tag(pb, "LIST");
  62. put_tag(pb, list_tag);
  63. return loff;
  64. }
  65. static unsigned char* avi_stream2fourcc(unsigned char* tag, int index,
  66. enum CodecType type)
  67. {
  68. tag[0] = '0';
  69. tag[1] = '0' + index;
  70. if (type == CODEC_TYPE_VIDEO) {
  71. tag[2] = 'd';
  72. tag[3] = 'c';
  73. } else {
  74. tag[2] = 'w';
  75. tag[3] = 'b';
  76. }
  77. tag[4] = '\0';
  78. return tag;
  79. }
  80. static void avi_write_info_tag(ByteIOContext *pb, const char *tag, const char *str)
  81. {
  82. int len = strlen(str);
  83. if (len > 0) {
  84. len++;
  85. put_tag(pb, tag);
  86. put_le32(pb, len);
  87. put_strz(pb, str);
  88. if (len & 1)
  89. put_byte(pb, 0);
  90. }
  91. }
  92. static int avi_write_header(AVFormatContext *s)
  93. {
  94. AVIContext *avi = s->priv_data;
  95. ByteIOContext *pb = &s->pb;
  96. int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
  97. AVCodecContext *stream, *video_enc;
  98. offset_t list1, list2, strh, strf;
  99. /* header list */
  100. avi->riff_id = 0;
  101. list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl");
  102. /* avi header */
  103. put_tag(pb, "avih");
  104. put_le32(pb, 14 * 4);
  105. bitrate = 0;
  106. video_enc = NULL;
  107. for(n=0;n<s->nb_streams;n++) {
  108. stream = s->streams[n]->codec;
  109. bitrate += stream->bit_rate;
  110. if (stream->codec_type == CODEC_TYPE_VIDEO)
  111. video_enc = stream;
  112. }
  113. nb_frames = 0;
  114. if(video_enc){
  115. put_le32(pb, (uint32_t)(int64_t_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
  116. } else {
  117. put_le32(pb, 0);
  118. }
  119. put_le32(pb, bitrate / 8); /* XXX: not quite exact */
  120. put_le32(pb, 0); /* padding */
  121. if (url_is_streamed(pb))
  122. put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
  123. else
  124. put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
  125. avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
  126. put_le32(pb, nb_frames); /* nb frames, filled later */
  127. put_le32(pb, 0); /* initial frame */
  128. put_le32(pb, s->nb_streams); /* nb streams */
  129. put_le32(pb, 1024 * 1024); /* suggested buffer size */
  130. if(video_enc){
  131. put_le32(pb, video_enc->width);
  132. put_le32(pb, video_enc->height);
  133. } else {
  134. put_le32(pb, 0);
  135. put_le32(pb, 0);
  136. }
  137. put_le32(pb, 0); /* reserved */
  138. put_le32(pb, 0); /* reserved */
  139. put_le32(pb, 0); /* reserved */
  140. put_le32(pb, 0); /* reserved */
  141. /* stream list */
  142. for(i=0;i<n;i++) {
  143. list2 = start_tag(pb, "LIST");
  144. put_tag(pb, "strl");
  145. stream = s->streams[i]->codec;
  146. /* FourCC should really be set by the codec itself */
  147. if (! stream->codec_tag) {
  148. stream->codec_tag = codec_get_bmp_tag(stream->codec_id);
  149. }
  150. /* stream generic header */
  151. strh = start_tag(pb, "strh");
  152. switch(stream->codec_type) {
  153. case CODEC_TYPE_VIDEO: put_tag(pb, "vids"); break;
  154. case CODEC_TYPE_AUDIO: put_tag(pb, "auds"); break;
  155. // case CODEC_TYPE_TEXT : put_tag(pb, "txts"); break;
  156. case CODEC_TYPE_DATA : put_tag(pb, "dats"); break;
  157. }
  158. if(stream->codec_type == CODEC_TYPE_VIDEO)
  159. put_le32(pb, stream->codec_tag);
  160. else
  161. put_le32(pb, 1);
  162. put_le32(pb, 0); /* flags */
  163. put_le16(pb, 0); /* priority */
  164. put_le16(pb, 0); /* language */
  165. put_le32(pb, 0); /* initial frame */
  166. ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
  167. put_le32(pb, au_scale); /* scale */
  168. put_le32(pb, au_byterate); /* rate */
  169. av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
  170. put_le32(pb, 0); /* start */
  171. avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
  172. if (url_is_streamed(pb))
  173. put_le32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
  174. else
  175. put_le32(pb, 0); /* length, XXX: filled later */
  176. /* suggested buffer size */ //FIXME set at the end to largest chunk
  177. if(stream->codec_type == CODEC_TYPE_VIDEO)
  178. put_le32(pb, 1024 * 1024);
  179. else if(stream->codec_type == CODEC_TYPE_AUDIO)
  180. put_le32(pb, 12 * 1024);
  181. else
  182. put_le32(pb, 0);
  183. put_le32(pb, -1); /* quality */
  184. put_le32(pb, au_ssize); /* sample size */
  185. put_le32(pb, 0);
  186. put_le16(pb, stream->width);
  187. put_le16(pb, stream->height);
  188. end_tag(pb, strh);
  189. if(stream->codec_type != CODEC_TYPE_DATA){
  190. strf = start_tag(pb, "strf");
  191. switch(stream->codec_type) {
  192. case CODEC_TYPE_VIDEO:
  193. put_bmp_header(pb, stream, codec_bmp_tags, 0);
  194. break;
  195. case CODEC_TYPE_AUDIO:
  196. if (put_wav_header(pb, stream) < 0) {
  197. av_free(avi);
  198. return -1;
  199. }
  200. break;
  201. default:
  202. return -1;
  203. }
  204. end_tag(pb, strf);
  205. }
  206. if (!url_is_streamed(pb)) {
  207. unsigned char tag[5];
  208. int j;
  209. /* Starting to lay out AVI OpenDML master index.
  210. * We want to make it JUNK entry for now, since we'd
  211. * like to get away without making AVI an OpenDML one
  212. * for compatibility reasons.
  213. */
  214. avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0;
  215. avi->indexes[i].indx_start = start_tag(pb, "JUNK");
  216. put_le16(pb, 4); /* wLongsPerEntry */
  217. put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
  218. put_byte(pb, 0); /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
  219. put_le32(pb, 0); /* nEntriesInUse (will fill out later on) */
  220. put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
  221. /* dwChunkId */
  222. put_le64(pb, 0); /* dwReserved[3]
  223. put_le32(pb, 0); Must be 0. */
  224. for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
  225. put_le64(pb, 0);
  226. end_tag(pb, avi->indexes[i].indx_start);
  227. }
  228. end_tag(pb, list2);
  229. }
  230. if (!url_is_streamed(pb)) {
  231. /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
  232. avi->odml_list = start_tag(pb, "JUNK");
  233. put_tag(pb, "odml");
  234. put_tag(pb, "dmlh");
  235. put_le32(pb, 248);
  236. for (i = 0; i < 248; i+= 4)
  237. put_le32(pb, 0);
  238. end_tag(pb, avi->odml_list);
  239. }
  240. end_tag(pb, list1);
  241. list2 = start_tag(pb, "LIST");
  242. put_tag(pb, "INFO");
  243. avi_write_info_tag(pb, "INAM", s->title);
  244. avi_write_info_tag(pb, "IART", s->author);
  245. avi_write_info_tag(pb, "ICOP", s->copyright);
  246. avi_write_info_tag(pb, "ICMT", s->comment);
  247. avi_write_info_tag(pb, "IPRD", s->album);
  248. avi_write_info_tag(pb, "IGNR", s->genre);
  249. if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
  250. avi_write_info_tag(pb, "ISFT", LIBAVFORMAT_IDENT);
  251. end_tag(pb, list2);
  252. /* some padding for easier tag editing */
  253. list2 = start_tag(pb, "JUNK");
  254. for (i = 0; i < 1016; i += 4)
  255. put_le32(pb, 0);
  256. end_tag(pb, list2);
  257. avi->movi_list = start_tag(pb, "LIST");
  258. put_tag(pb, "movi");
  259. put_flush_packet(pb);
  260. return 0;
  261. }
  262. static int avi_write_ix(AVFormatContext *s)
  263. {
  264. ByteIOContext *pb = &s->pb;
  265. AVIContext *avi = s->priv_data;
  266. unsigned char tag[5];
  267. unsigned char ix_tag[] = "ix00";
  268. int i, j;
  269. assert(!url_is_streamed(pb));
  270. if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
  271. return -1;
  272. for (i=0;i<s->nb_streams;i++) {
  273. offset_t ix, pos;
  274. avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
  275. ix_tag[3] = '0' + i;
  276. /* Writing AVI OpenDML leaf index chunk */
  277. ix = url_ftell(pb);
  278. put_tag(pb, &ix_tag[0]); /* ix?? */
  279. put_le32(pb, avi->indexes[i].entry * 8 + 24);
  280. /* chunk size */
  281. put_le16(pb, 2); /* wLongsPerEntry */
  282. put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
  283. put_byte(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
  284. put_le32(pb, avi->indexes[i].entry);
  285. /* nEntriesInUse */
  286. put_tag(pb, &tag[0]); /* dwChunkId */
  287. put_le64(pb, avi->movi_list);/* qwBaseOffset */
  288. put_le32(pb, 0); /* dwReserved_3 (must be 0) */
  289. for (j=0; j<avi->indexes[i].entry; j++) {
  290. AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j);
  291. put_le32(pb, ie->pos + 8);
  292. put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
  293. (ie->flags & 0x10 ? 0 : 0x80000000));
  294. }
  295. put_flush_packet(pb);
  296. pos = url_ftell(pb);
  297. /* Updating one entry in the AVI OpenDML master index */
  298. url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET);
  299. put_tag(pb, "indx"); /* enabling this entry */
  300. url_fskip(pb, 8);
  301. put_le32(pb, avi->riff_id); /* nEntriesInUse */
  302. url_fskip(pb, 16*avi->riff_id);
  303. put_le64(pb, ix); /* qwOffset */
  304. put_le32(pb, pos - ix); /* dwSize */
  305. put_le32(pb, avi->indexes[i].entry); /* dwDuration */
  306. url_fseek(pb, pos, SEEK_SET);
  307. }
  308. return 0;
  309. }
  310. static int avi_write_idx1(AVFormatContext *s)
  311. {
  312. ByteIOContext *pb = &s->pb;
  313. AVIContext *avi = s->priv_data;
  314. offset_t file_size, idx_chunk;
  315. int i, n, nb_frames, au_byterate, au_ssize, au_scale;
  316. AVCodecContext *stream;
  317. unsigned char tag[5];
  318. if (!url_is_streamed(pb)) {
  319. AVIIentry* ie = 0, *tie;
  320. int entry[MAX_STREAMS];
  321. int empty, stream_id = -1;
  322. idx_chunk = start_tag(pb, "idx1");
  323. memset(&entry[0], 0, sizeof(entry));
  324. do {
  325. empty = 1;
  326. for (i=0; i<s->nb_streams; i++) {
  327. if (avi->indexes[i].entry <= entry[i])
  328. continue;
  329. tie = avi_get_ientry(&avi->indexes[i], entry[i]);
  330. if (empty || tie->pos < ie->pos) {
  331. ie = tie;
  332. stream_id = i;
  333. }
  334. empty = 0;
  335. }
  336. if (!empty) {
  337. avi_stream2fourcc(&tag[0], stream_id,
  338. s->streams[stream_id]->codec->codec_type);
  339. put_tag(pb, &tag[0]);
  340. put_le32(pb, ie->flags);
  341. put_le32(pb, ie->pos);
  342. put_le32(pb, ie->len);
  343. entry[stream_id]++;
  344. }
  345. } while (!empty);
  346. end_tag(pb, idx_chunk);
  347. /* Fill in frame/sample counters */
  348. file_size = url_ftell(pb);
  349. nb_frames = 0;
  350. for(n=0;n<s->nb_streams;n++) {
  351. assert(avi->frames_hdr_strm[n]);
  352. stream = s->streams[n]->codec;
  353. url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
  354. ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
  355. if (au_ssize == 0) {
  356. put_le32(pb, avi->packet_count[n]);
  357. } else {
  358. put_le32(pb, avi->audio_strm_length[n] / au_ssize);
  359. }
  360. if(stream->codec_type == CODEC_TYPE_VIDEO)
  361. nb_frames = FFMAX(nb_frames, avi->packet_count[n]);
  362. }
  363. assert(avi->frames_hdr_all);
  364. url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
  365. put_le32(pb, nb_frames);
  366. url_fseek(pb, file_size, SEEK_SET);
  367. }
  368. return 0;
  369. }
  370. static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
  371. {
  372. AVIContext *avi = s->priv_data;
  373. ByteIOContext *pb = &s->pb;
  374. unsigned char tag[5];
  375. unsigned int flags=0;
  376. const int stream_index= pkt->stream_index;
  377. AVCodecContext *enc= s->streams[stream_index]->codec;
  378. int size= pkt->size;
  379. // av_log(s, AV_LOG_DEBUG, "%lld %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
  380. while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avi->packet_count[stream_index]){
  381. AVPacket empty_packet;
  382. av_init_packet(&empty_packet);
  383. empty_packet.size= 0;
  384. empty_packet.data= NULL;
  385. empty_packet.stream_index= stream_index;
  386. avi_write_packet(s, &empty_packet);
  387. // av_log(s, AV_LOG_DEBUG, "dup %lld %d\n", pkt->dts, avi->packet_count[stream_index]);
  388. }
  389. avi->packet_count[stream_index]++;
  390. // Make sure to put an OpenDML chunk when the file size exceeds the limits
  391. if (!url_is_streamed(pb) &&
  392. (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
  393. avi_write_ix(s);
  394. end_tag(pb, avi->movi_list);
  395. if (avi->riff_id == 1)
  396. avi_write_idx1(s);
  397. end_tag(pb, avi->riff_start);
  398. avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi");
  399. }
  400. avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
  401. if(pkt->flags&PKT_FLAG_KEY)
  402. flags = 0x10;
  403. if (enc->codec_type == CODEC_TYPE_AUDIO) {
  404. avi->audio_strm_length[stream_index] += size;
  405. }
  406. if (!url_is_streamed(&s->pb)) {
  407. AVIIndex* idx = &avi->indexes[stream_index];
  408. int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
  409. int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
  410. if (idx->ents_allocated <= idx->entry) {
  411. idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
  412. if (!idx->cluster)
  413. return -1;
  414. idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
  415. if (!idx->cluster[cl])
  416. return -1;
  417. idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
  418. }
  419. idx->cluster[cl][id].flags = flags;
  420. idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
  421. idx->cluster[cl][id].len = size;
  422. idx->entry++;
  423. }
  424. put_buffer(pb, tag, 4);
  425. put_le32(pb, size);
  426. put_buffer(pb, pkt->data, size);
  427. if (size & 1)
  428. put_byte(pb, 0);
  429. put_flush_packet(pb);
  430. return 0;
  431. }
  432. static int avi_write_trailer(AVFormatContext *s)
  433. {
  434. AVIContext *avi = s->priv_data;
  435. ByteIOContext *pb = &s->pb;
  436. int res = 0;
  437. int i, j, n, nb_frames;
  438. offset_t file_size;
  439. if (!url_is_streamed(pb))
  440. {
  441. if (avi->riff_id == 1) {
  442. end_tag(pb, avi->movi_list);
  443. res = avi_write_idx1(s);
  444. end_tag(pb, avi->riff_start);
  445. } else {
  446. avi_write_ix(s);
  447. end_tag(pb, avi->movi_list);
  448. end_tag(pb, avi->riff_start);
  449. file_size = url_ftell(pb);
  450. url_fseek(pb, avi->odml_list - 8, SEEK_SET);
  451. put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
  452. url_fskip(pb, 16);
  453. for (n=nb_frames=0;n<s->nb_streams;n++) {
  454. AVCodecContext *stream = s->streams[n]->codec;
  455. if (stream->codec_type == CODEC_TYPE_VIDEO) {
  456. if (nb_frames < avi->packet_count[n])
  457. nb_frames = avi->packet_count[n];
  458. } else {
  459. if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
  460. nb_frames += avi->packet_count[n];
  461. }
  462. }
  463. }
  464. put_le32(pb, nb_frames);
  465. url_fseek(pb, file_size, SEEK_SET);
  466. }
  467. }
  468. put_flush_packet(pb);
  469. for (i=0; i<MAX_STREAMS; i++) {
  470. for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
  471. av_free(avi->indexes[i].cluster[j]);
  472. av_free(avi->indexes[i].cluster);
  473. avi->indexes[i].cluster = NULL;
  474. avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0;
  475. }
  476. return res;
  477. }
  478. AVOutputFormat avi_muxer = {
  479. "avi",
  480. "avi format",
  481. "video/x-msvideo",
  482. "avi",
  483. sizeof(AVIContext),
  484. CODEC_ID_MP2,
  485. CODEC_ID_MPEG4,
  486. avi_write_header,
  487. avi_write_packet,
  488. avi_write_trailer,
  489. };
  490. #endif //CONFIG_AVI_MUXER