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.

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