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.

576 lines
18KB

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