📄 matroskaenc.c
字号:
/* * Matroska muxer * Copyright (c) 2007 David Conrad * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */#include "avformat.h"#include "md5.h"#include "riff.h"#include "xiph.h"#include "matroska.h"typedef struct ebml_master { offset_t pos; ///< absolute offset in the file where the master's elements start int sizebytes; ///< how many bytes were reserved for the size} ebml_master;typedef struct mkv_seekhead_entry { unsigned int elementid; uint64_t segmentpos;} mkv_seekhead_entry;typedef struct mkv_seekhead { offset_t filepos; offset_t segment_offset; ///< the file offset to the beginning of the segment int reserved_size; ///< -1 if appending to file int max_entries; mkv_seekhead_entry *entries; int num_entries;} mkv_seekhead;typedef struct { uint64_t pts; int tracknum; offset_t cluster_pos; ///< file offset of the cluster containing the block} mkv_cuepoint;typedef struct { offset_t segment_offset; mkv_cuepoint *entries; int num_entries;} mkv_cues;typedef struct MatroskaMuxContext { ebml_master segment; offset_t segment_offset; offset_t segment_uid; ebml_master cluster; offset_t cluster_pos; ///< file offset of the current cluster uint64_t cluster_pts; offset_t duration_offset; uint64_t duration; mkv_seekhead *main_seekhead; mkv_seekhead *cluster_seekhead; mkv_cues *cues; struct AVMD5 *md5_ctx;} MatroskaMuxContext;/** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit * offset, 4 bytes for target EBML ID */#define MAX_SEEKENTRY_SIZE 21/** per-cuepoint-track - 3 1-byte EBML IDs, 3 1-byte EBML sizes, 2 * 8-byte uint max */#define MAX_CUETRACKPOS_SIZE 22/** per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max */#define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE*num_tracksstatic int ebml_id_size(unsigned int id){ return (av_log2(id+1)-1)/7+1;}static void put_ebml_id(ByteIOContext *pb, unsigned int id){ int i = ebml_id_size(id); while (i--) put_byte(pb, id >> (i*8));}/** * Write an EBML size meaning "unknown size". * * @param bytes The number of bytes the size should occupy (maximum: 8). */static void put_ebml_size_unknown(ByteIOContext *pb, int bytes){ assert(bytes <= 8); put_byte(pb, 0x1ff >> bytes); while (--bytes) put_byte(pb, 0xff);}/** * Calculate how many bytes are needed to represent a given number in EBML. */static int ebml_num_size(uint64_t num){ int bytes = 1; while ((num+1) >> bytes*7) bytes++; return bytes;}/** * Write a number in EBML variable length format. * * @param bytes The number of bytes that need to be used to write the number. * If zero, any number of bytes can be used. */static void put_ebml_num(ByteIOContext *pb, uint64_t num, int bytes){ int i, needed_bytes = ebml_num_size(num); // sizes larger than this are currently undefined in EBML assert(num < (1ULL<<56)-1); if (bytes == 0) // don't care how many bytes are used, so use the min bytes = needed_bytes; // the bytes needed to write the given size would exceed the bytes // that we need to use, so write unknown size. This shouldn't happen. assert(bytes >= needed_bytes); num |= 1ULL << bytes*7; for (i = bytes - 1; i >= 0; i--) put_byte(pb, num >> i*8);}static void put_ebml_uint(ByteIOContext *pb, unsigned int elementid, uint64_t val){ int i, bytes = 1; while (val >> bytes*8) bytes++; put_ebml_id(pb, elementid); put_ebml_num(pb, bytes, 0); for (i = bytes - 1; i >= 0; i--) put_byte(pb, val >> i*8);}static void put_ebml_float(ByteIOContext *pb, unsigned int elementid, double val){ put_ebml_id(pb, elementid); put_ebml_num(pb, 8, 0); put_be64(pb, av_dbl2int(val));}static void put_ebml_binary(ByteIOContext *pb, unsigned int elementid, const uint8_t *buf, int size){ put_ebml_id(pb, elementid); put_ebml_num(pb, size, 0); put_buffer(pb, buf, size);}static void put_ebml_string(ByteIOContext *pb, unsigned int elementid, const char *str){ put_ebml_binary(pb, elementid, str, strlen(str));}/** * Writes a void element of a given size. Useful for reserving space in * the file to be written to later. * * @param size The number of bytes to reserve, which must be at least 2. */static void put_ebml_void(ByteIOContext *pb, uint64_t size){ offset_t currentpos = url_ftell(pb); assert(size >= 2); put_ebml_id(pb, EBML_ID_VOID); // we need to subtract the length needed to store the size from the // size we need to reserve so 2 cases, we use 8 bytes to store the // size if possible, 1 byte otherwise if (size < 10) put_ebml_num(pb, size-1, 0); else put_ebml_num(pb, size-9, 8); url_fseek(pb, currentpos + size, SEEK_SET);}static ebml_master start_ebml_master(ByteIOContext *pb, unsigned int elementid, uint64_t expectedsize){ int bytes = expectedsize ? ebml_num_size(expectedsize) : 8; put_ebml_id(pb, elementid); put_ebml_size_unknown(pb, bytes); return (ebml_master){ url_ftell(pb), bytes };}static void end_ebml_master(ByteIOContext *pb, ebml_master master){ offset_t pos = url_ftell(pb); // leave the unknown size for masters when streaming if (url_is_streamed(pb)) return; url_fseek(pb, master.pos - master.sizebytes, SEEK_SET); put_ebml_num(pb, pos - master.pos, master.sizebytes); url_fseek(pb, pos, SEEK_SET);}static void put_xiph_size(ByteIOContext *pb, int size){ int i; for (i = 0; i < size / 255; i++) put_byte(pb, 255); put_byte(pb, size % 255);}/** * Initialize a mkv_seekhead element to be ready to index level 1 Matroska * elements. If a maximum number of elements is specified, enough space * will be reserved at the current file location to write a seek head of * that size. * * @param segment_offset The absolute offset to the position in the file * where the segment begins. * @param numelements The maximum number of elements that will be indexed * by this seek head, 0 if unlimited. */static mkv_seekhead * mkv_start_seekhead(ByteIOContext *pb, offset_t segment_offset, int numelements){ mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead)); if (new_seekhead == NULL) return NULL; new_seekhead->segment_offset = segment_offset; if (numelements > 0) { new_seekhead->filepos = url_ftell(pb); // 21 bytes max for a seek entry, 10 bytes max for the SeekHead ID // and size, and 3 bytes to guarantee that an EBML void element // will fit afterwards new_seekhead->reserved_size = numelements * MAX_SEEKENTRY_SIZE + 13; new_seekhead->max_entries = numelements; put_ebml_void(pb, new_seekhead->reserved_size); } return new_seekhead;}static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos){ mkv_seekhead_entry *entries = seekhead->entries; // don't store more elements than we reserved space for if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries) return -1; entries = av_realloc(entries, (seekhead->num_entries + 1) * sizeof(mkv_seekhead_entry)); if (entries == NULL) return AVERROR(ENOMEM); entries[seekhead->num_entries ].elementid = elementid; entries[seekhead->num_entries++].segmentpos = filepos - seekhead->segment_offset; seekhead->entries = entries; return 0;}/** * Write the seek head to the file and free it. If a maximum number of * elements was specified to mkv_start_seekhead(), the seek head will * be written at the location reserved for it. Otherwise, it is written * at the current location in the file. * * @return The file offset where the seekhead was written. */static offset_t mkv_write_seekhead(ByteIOContext *pb, mkv_seekhead *seekhead){ ebml_master metaseek, seekentry; offset_t currentpos; int i; currentpos = url_ftell(pb); if (seekhead->reserved_size > 0) url_fseek(pb, seekhead->filepos, SEEK_SET); metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size); for (i = 0; i < seekhead->num_entries; i++) { mkv_seekhead_entry *entry = &seekhead->entries[i]; seekentry = start_ebml_master(pb, MATROSKA_ID_SEEKENTRY, MAX_SEEKENTRY_SIZE); put_ebml_id(pb, MATROSKA_ID_SEEKID); put_ebml_num(pb, ebml_id_size(entry->elementid), 0); put_ebml_id(pb, entry->elementid); put_ebml_uint(pb, MATROSKA_ID_SEEKPOSITION, entry->segmentpos); end_ebml_master(pb, seekentry); } end_ebml_master(pb, metaseek); if (seekhead->reserved_size > 0) { uint64_t remaining = seekhead->filepos + seekhead->reserved_size - url_ftell(pb); put_ebml_void(pb, remaining); url_fseek(pb, currentpos, SEEK_SET); currentpos = seekhead->filepos; } av_free(seekhead->entries); av_free(seekhead); return currentpos;}static mkv_cues * mkv_start_cues(offset_t segment_offset){ mkv_cues *cues = av_mallocz(sizeof(mkv_cues)); if (cues == NULL) return NULL; cues->segment_offset = segment_offset; return cues;}static int mkv_add_cuepoint(mkv_cues *cues, AVPacket *pkt, offset_t cluster_pos){ mkv_cuepoint *entries = cues->entries; entries = av_realloc(entries, (cues->num_entries + 1) * sizeof(mkv_cuepoint)); if (entries == NULL) return AVERROR(ENOMEM); entries[cues->num_entries ].pts = pkt->pts; entries[cues->num_entries ].tracknum = pkt->stream_index + 1; entries[cues->num_entries++].cluster_pos = cluster_pos - cues->segment_offset; cues->entries = entries; return 0;}static offset_t mkv_write_cues(ByteIOContext *pb, mkv_cues *cues, int num_tracks){ ebml_master cues_element; offset_t currentpos; int i, j; currentpos = url_ftell(pb); cues_element = start_ebml_master(pb, MATROSKA_ID_CUES, 0); for (i = 0; i < cues->num_entries; i++) { ebml_master cuepoint, track_positions; mkv_cuepoint *entry = &cues->entries[i]; uint64_t pts = entry->pts; cuepoint = start_ebml_master(pb, MATROSKA_ID_POINTENTRY, MAX_CUEPOINT_SIZE(num_tracks)); put_ebml_uint(pb, MATROSKA_ID_CUETIME, pts); // put all the entries from different tracks that have the exact same // timestamp into the same CuePoint for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) { track_positions = start_ebml_master(pb, MATROSKA_ID_CUETRACKPOSITION, MAX_CUETRACKPOS_SIZE); put_ebml_uint(pb, MATROSKA_ID_CUETRACK , entry[j].tracknum ); put_ebml_uint(pb, MATROSKA_ID_CUECLUSTERPOSITION, entry[j].cluster_pos); end_ebml_master(pb, track_positions); } i += j - 1; end_ebml_master(pb, cuepoint); } end_ebml_master(pb, cues_element); av_free(cues->entries); av_free(cues); return currentpos;}static int put_xiph_codecpriv(AVFormatContext *s, ByteIOContext *pb, AVCodecContext *codec){ uint8_t *header_start[3]; int header_len[3]; int first_header_size; int j; if (codec->codec_id == CODEC_ID_VORBIS) first_header_size = 30; else first_header_size = 42; if (ff_split_xiph_headers(codec->extradata, codec->extradata_size, first_header_size, header_start, header_len) < 0) { av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n"); return -1; } put_byte(pb, 2); // number packets - 1 for (j = 0; j < 2; j++) { put_xiph_size(pb, header_len[j]); } for (j = 0; j < 3; j++) put_buffer(pb, header_start[j], header_len[j]); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -