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.

580 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->track) {
  279. char str_track[4];
  280. snprintf(str_track, 4, "%d", s->track);
  281. avi_write_info_tag(pb, "IPRT", str_track);
  282. }
  283. if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
  284. avi_write_info_tag(pb, "ISFT", LIBAVFORMAT_IDENT);
  285. end_tag(pb, list2);
  286. /* some padding for easier tag editing */
  287. list2 = start_tag(pb, "JUNK");
  288. for (i = 0; i < 1016; i += 4)
  289. put_le32(pb, 0);
  290. end_tag(pb, list2);
  291. avi->movi_list = start_tag(pb, "LIST");
  292. put_tag(pb, "movi");
  293. put_flush_packet(pb);
  294. return 0;
  295. }
  296. static int avi_write_ix(AVFormatContext *s)
  297. {
  298. ByteIOContext *pb = &s->pb;
  299. AVIContext *avi = s->priv_data;
  300. unsigned char tag[5];
  301. unsigned char ix_tag[] = "ix00";
  302. int i, j;
  303. assert(!url_is_streamed(pb));
  304. if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
  305. return -1;
  306. for (i=0;i<s->nb_streams;i++) {
  307. offset_t ix, pos;
  308. avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
  309. ix_tag[3] = '0' + i;
  310. /* Writing AVI OpenDML leaf index chunk */
  311. ix = url_ftell(pb);
  312. put_tag(pb, &ix_tag[0]); /* ix?? */
  313. put_le32(pb, avi->indexes[i].entry * 8 + 24);
  314. /* chunk size */
  315. put_le16(pb, 2); /* wLongsPerEntry */
  316. put_byte(pb, 0); /* bIndexSubType (0 == frame index) */
  317. put_byte(pb, 1); /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
  318. put_le32(pb, avi->indexes[i].entry);
  319. /* nEntriesInUse */
  320. put_tag(pb, &tag[0]); /* dwChunkId */
  321. put_le64(pb, avi->movi_list);/* qwBaseOffset */
  322. put_le32(pb, 0); /* dwReserved_3 (must be 0) */
  323. for (j=0; j<avi->indexes[i].entry; j++) {
  324. AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j);
  325. put_le32(pb, ie->pos + 8);
  326. put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
  327. (ie->flags & 0x10 ? 0 : 0x80000000));
  328. }
  329. put_flush_packet(pb);
  330. pos = url_ftell(pb);
  331. /* Updating one entry in the AVI OpenDML master index */
  332. url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET);
  333. put_tag(pb, "indx"); /* enabling this entry */
  334. url_fskip(pb, 8);
  335. put_le32(pb, avi->riff_id); /* nEntriesInUse */
  336. url_fskip(pb, 16*avi->riff_id);
  337. put_le64(pb, ix); /* qwOffset */
  338. put_le32(pb, pos - ix); /* dwSize */
  339. put_le32(pb, avi->indexes[i].entry); /* dwDuration */
  340. url_fseek(pb, pos, SEEK_SET);
  341. }
  342. return 0;
  343. }
  344. static int avi_write_idx1(AVFormatContext *s)
  345. {
  346. ByteIOContext *pb = &s->pb;
  347. AVIContext *avi = s->priv_data;
  348. offset_t idx_chunk;
  349. int i;
  350. unsigned char tag[5];
  351. if (!url_is_streamed(pb)) {
  352. AVIIentry* ie = 0, *tie;
  353. int entry[MAX_STREAMS];
  354. int empty, stream_id = -1;
  355. idx_chunk = start_tag(pb, "idx1");
  356. memset(&entry[0], 0, sizeof(entry));
  357. do {
  358. empty = 1;
  359. for (i=0; i<s->nb_streams; i++) {
  360. if (avi->indexes[i].entry <= entry[i])
  361. continue;
  362. tie = avi_get_ientry(&avi->indexes[i], entry[i]);
  363. if (empty || tie->pos < ie->pos) {
  364. ie = tie;
  365. stream_id = i;
  366. }
  367. empty = 0;
  368. }
  369. if (!empty) {
  370. avi_stream2fourcc(&tag[0], stream_id,
  371. s->streams[stream_id]->codec->codec_type);
  372. put_tag(pb, &tag[0]);
  373. put_le32(pb, ie->flags);
  374. put_le32(pb, ie->pos);
  375. put_le32(pb, ie->len);
  376. entry[stream_id]++;
  377. }
  378. } while (!empty);
  379. end_tag(pb, idx_chunk);
  380. avi_write_counters(s, avi->riff_id);
  381. }
  382. return 0;
  383. }
  384. static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
  385. {
  386. AVIContext *avi = s->priv_data;
  387. ByteIOContext *pb = &s->pb;
  388. unsigned char tag[5];
  389. unsigned int flags=0;
  390. const int stream_index= pkt->stream_index;
  391. AVCodecContext *enc= s->streams[stream_index]->codec;
  392. int size= pkt->size;
  393. // av_log(s, AV_LOG_DEBUG, "%lld %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
  394. while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avi->packet_count[stream_index]){
  395. AVPacket empty_packet;
  396. av_init_packet(&empty_packet);
  397. empty_packet.size= 0;
  398. empty_packet.data= NULL;
  399. empty_packet.stream_index= stream_index;
  400. avi_write_packet(s, &empty_packet);
  401. // av_log(s, AV_LOG_DEBUG, "dup %lld %d\n", pkt->dts, avi->packet_count[stream_index]);
  402. }
  403. avi->packet_count[stream_index]++;
  404. // Make sure to put an OpenDML chunk when the file size exceeds the limits
  405. if (!url_is_streamed(pb) &&
  406. (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
  407. avi_write_ix(s);
  408. end_tag(pb, avi->movi_list);
  409. if (avi->riff_id == 1)
  410. avi_write_idx1(s);
  411. end_tag(pb, avi->riff_start);
  412. avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi");
  413. }
  414. avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
  415. if(pkt->flags&PKT_FLAG_KEY)
  416. flags = 0x10;
  417. if (enc->codec_type == CODEC_TYPE_AUDIO) {
  418. avi->audio_strm_length[stream_index] += size;
  419. }
  420. if (!url_is_streamed(&s->pb)) {
  421. AVIIndex* idx = &avi->indexes[stream_index];
  422. int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
  423. int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
  424. if (idx->ents_allocated <= idx->entry) {
  425. idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
  426. if (!idx->cluster)
  427. return -1;
  428. idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
  429. if (!idx->cluster[cl])
  430. return -1;
  431. idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
  432. }
  433. idx->cluster[cl][id].flags = flags;
  434. idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
  435. idx->cluster[cl][id].len = size;
  436. idx->entry++;
  437. }
  438. put_buffer(pb, tag, 4);
  439. put_le32(pb, size);
  440. put_buffer(pb, pkt->data, size);
  441. if (size & 1)
  442. put_byte(pb, 0);
  443. put_flush_packet(pb);
  444. return 0;
  445. }
  446. static int avi_write_trailer(AVFormatContext *s)
  447. {
  448. AVIContext *avi = s->priv_data;
  449. ByteIOContext *pb = &s->pb;
  450. int res = 0;
  451. int i, j, n, nb_frames;
  452. offset_t file_size;
  453. if (!url_is_streamed(pb))
  454. {
  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. };
  505. #endif //CONFIG_AVI_MUXER