From d59ccdb2a24fe7a76bbede6d320e2d01791b927c Mon Sep 17 00:00:00 2001 From: John Stebbins Date: Mon, 12 Jan 2009 20:05:23 -0800 Subject: [PATCH] add attachment support --- include/libmkv.h | 3 ++- src/Makefile.am | 2 +- src/attachments.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/matroska.c | 8 ++++++ src/matroska.h | 4 +++ 5 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/attachments.c diff --git a/include/libmkv.h b/include/libmkv.h index 89f60ce..20608b1 100644 --- a/include/libmkv.h +++ b/include/libmkv.h @@ -235,7 +235,8 @@ char *mk_laceXiph(uint64_t *sizes, uint8_t num_frames, int mk_createTagSimple(mk_Writer *w, char *tag_id, char *value); int mk_createTagSimpleBin(mk_Writer *w, char *tag_id, const void *data, unsigned size); -int mk_writeTags(mk_Writer *w); +int mk_createAttachment( mk_Writer * w, char *name, char *description, + char *mime, const void *data, unsigned size); #ifdef __cplusplus } diff --git a/src/Makefile.am b/src/Makefile.am index 3201a9d..1981ce5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,4 +3,4 @@ INCLUDES = -I$(top_builddir) -I$(top_srcdir) -I$(top_srcdir)/include lib_LTLIBRARIES = libmkv.la -libmkv_la_SOURCES = matroska.c matroska.h tracks.c chapters.c ebml.h ebml.c md5.h md5.c tags.c +libmkv_la_SOURCES = matroska.c matroska.h tracks.c chapters.c ebml.h ebml.c md5.h md5.c tags.c attachments.c diff --git a/src/attachments.c b/src/attachments.c new file mode 100644 index 0000000..0f730a7 --- /dev/null +++ b/src/attachments.c @@ -0,0 +1,78 @@ +/***************************************************************************** + * attachments.c: + ***************************************************************************** + * Copyright (C) 2007 libmkv + * + * Authors: John A. Stebbins + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + *****************************************************************************/ +#include "config.h" +#include "libmkv.h" +#include "matroska.h" + +int mk_createAttachment( + mk_Writer * w, + char *name, + char *description, + char *mime, + const void *data, + unsigned size) +{ + mk_Context *attach; + unsigned long file_uid; + /* + * Generate a random UID for this Attachment. + * NOTE: This probably should be a CRC32 of file data. + * In place of being completely random. + */ + file_uid = random(); + +printf("2att %p\n", w->attachments); + if (w->attachments == NULL) { +printf("create attachments context\n"); + /* Attachments */ + if ((w->attachments = mk_createContext(w, w->root, MATROSKA_ID_ATTACHMENTS)) == NULL) + return -1; + } + /* AttachedFile */ +printf("create attachedfile context\n"); + if ((attach = mk_createContext(w, w->attachments, MATROSKA_ID_ATTACHEDFILE)) == NULL) + return -1; +printf("done attachedfile context\n"); + + /* FileName */ + CHECK(mk_writeStr(attach, MATROSKA_ID_FILENAME, name)); + /* FileName */ + CHECK(mk_writeStr(attach, MATROSKA_ID_FILEDESCRIPTION, description)); + /* FileMimeType */ + CHECK(mk_writeStr(attach, MATROSKA_ID_FILEMIMETYPE, mime)); + /* FileUID */ + CHECK(mk_writeUInt(attach, MATROSKA_ID_FILEUID, file_uid)); + /* FileData */ + CHECK(mk_writeBin(attach, MATROSKA_ID_FILEDATA, data, size)); + CHECK(mk_closeContext(attach, 0)); + + return 0; +} + +int mk_writeAttachments(mk_Writer * w) +{ + if (w->attachments == NULL) + return -1; + CHECK(mk_closeContext(w->attachments, 0)); + + return 0; +} diff --git a/src/matroska.c b/src/matroska.c index fd8cbd9..ee05390 100644 --- a/src/matroska.c +++ b/src/matroska.c @@ -90,6 +90,7 @@ mk_Writer *mk_createWriter(const char *filename, int64_t timescale, free(w); return NULL; } +printf("att %p\n", w->attachments); /* Cues */ if ((w->cues = mk_createContext(w, w->root, MATROSKA_ID_CUES)) == NULL) @@ -485,6 +486,13 @@ int mk_close(mk_Writer *w) ret = -1; } + if (w->attachments != NULL) { + w->seek_data.attachments = w->f_pos - w->segment_ptr; + mk_writeAttachments(w); + if (mk_flushContextData(w->root) < 0) + ret = -1; + } + if (w->tags != NULL) { w->seek_data.tags = w->f_pos - w->segment_ptr; mk_writeTags(w); diff --git a/src/matroska.h b/src/matroska.h index 665f27c..5dcd13a 100644 --- a/src/matroska.h +++ b/src/matroska.h @@ -135,6 +135,7 @@ /* IDs in the attachments master */ #define MATROSKA_ID_ATTACHEDFILE 0x61A7 /* sub-elements */ +#define MATROSKA_ID_FILEDESCRIPTION 0x467E /* UTF-8 */ #define MATROSKA_ID_FILENAME 0x466E /* UTF-8 */ #define MATROSKA_ID_FILEMIMETYPE 0x4660 /* string */ #define MATROSKA_ID_FILEDATA 0x465C /* binary */ @@ -222,6 +223,7 @@ struct mk_Writer_s { mk_Context *chapters; mk_Context *edition_entry; mk_Context *tags; + mk_Context *attachments; mk_Context *tag; mk_Context *tracks; mk_Context *cues; @@ -283,6 +285,8 @@ int mk_writeSeek(mk_Writer *w, mk_Context *c, unsigned seek_id, int mk_writeSeekHead(mk_Writer *w, int64_t *pointer); int mk_writeTracks(mk_Writer *w, mk_Context *tracks); int mk_writeChapters(mk_Writer *w); +int mk_writeTags(mk_Writer *w); +int mk_writeAttachments(mk_Writer *w); int mk_seekFile(mk_Writer *w, uint64_t pos); #endif -- 2.11.4.GIT