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.

530 lines
17KB

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