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.

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