ref: af9c532f7dc7fe59315250ca370358007bc55c9a
parent: 2ff38aefff2e4056c06aff9554b222b1687fe443
author: Jean-Marc Valin <[email protected]>
date: Wed Apr 26 22:32:50 EDT 2017
writing comment
--- a/src/opus_header.c
+++ b/src/opus_header.c
@@ -191,10 +191,7 @@
int user_comment_list_length=0;
int len=8+4+vendor_length+4;
char *p=(char*)malloc(len);
- if(p==NULL){
- fprintf(stderr, "malloc failed in comment_init()\n");
- exit(1);
- }
+ if (p == NULL) return;
memcpy(p, "OpusTags", 8);
writeint(p, 8, vendor_length);
memcpy(p+12, vendor_string, vendor_length);
@@ -213,10 +210,7 @@
int len=(*length)+4+tag_len+val_len;
p=(char*)realloc(p, len);
- if(p==NULL){
- fprintf(stderr, "realloc failed in comment_add()\n");
- exit(1);
- }
+ if (p == NULL) return;
writeint(p, *length, tag_len+val_len); /* length of comment */
if(tag){
@@ -239,10 +233,7 @@
round up to the maximum that fits in the current ogg segments.*/
newlen=(*length+amount+255)/255*255-1;
p=realloc(p,newlen);
- if(p==NULL){
- fprintf(stderr,"realloc failed in comment_pad()\n");
- exit(1);
- }
+ if (p == NULL) return;
for(i=*length;i<newlen;i++)p[i]=0;
*comments=p;
*length=newlen;
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -44,13 +44,14 @@
static int oe_write_page(ogg_page *page, OpusEncCallbacks *cb, void *user_data)
{
- int written;
- written=cb->write(user_data, page->header, page->header_len);
- written+=cb->write(user_data, page->body, page->body_len);
- return written;
+ int err;
+ err = cb->write(user_data, page->header, page->header_len);
+ if (err) return -1;
+ err = cb->write(user_data, page->body, page->body_len);
+ if (err) return -1;
+ return page->header_len+page->body_len;
}
-
struct StdioObject {
FILE *file;
};
@@ -65,8 +66,25 @@
ogg_page og;
ogg_packet op;
OpusHeader header;
+ char *comment;
+ int comment_length;
};
+static int oe_flush_page(OggOpusEnc *enc) {
+ ogg_page og;
+ int ret;
+ int written = 0;
+ while ( (ret = ogg_stream_flush(&enc->os, &og)) ) {
+ if (!ret) break;
+ ret = oe_write_page(&og, &enc->callbacks, enc->user_data);
+ if (ret == -1) {
+ return -1;
+ }
+ written += ret;
+ }
+ return written;
+}
+
int stdio_write(void *user_data, const unsigned char *ptr, int len) {
struct StdioObject *obj = (struct StdioObject*)user_data;
return fwrite(ptr, 1, len, obj->file) != (size_t)len;
@@ -129,6 +147,15 @@
}
if ( (enc = malloc(sizeof(*enc))) == NULL) goto fail;
enc->os_allocated = 0;
+ enc->comment = NULL;
+ comment_init(&enc->comment, &enc->comment_length, opus_get_version_string());
+ {
+ char encoder_string[1024];
+ snprintf(encoder_string, sizeof(encoder_string), "libopusenc version %s %s",PACKAGE_NAME,PACKAGE_VERSION);
+ comment_add(&enc->comment, &enc->comment_length, "ENCODER", encoder_string);
+ comment_pad(&enc->comment, &enc->comment_length, 512);
+ }
+ if (enc->comment == NULL) goto fail;
if ( (enc->buffer = malloc(sizeof(*enc->buffer)*BUFFER_SAMPLES*channels)) == NULL) goto fail;
enc->st = st;
enc->callbacks = *callbacks;
@@ -161,9 +188,7 @@
/*Write header*/
{
- int ret;
ogg_packet op;
- ogg_page og;
/*The Identification Header is 19 bytes, plus a Channel Mapping Table for
mapping families other than 0. The Channel Mapping Table is 2 bytes +
1 byte per channel. Because the maximum number of channels is 255, the
@@ -177,25 +202,16 @@
op.granulepos=0;
op.packetno=0;
ogg_stream_packetin(&enc->os, &op);
+ oe_flush_page(enc);
- while ( (ret = ogg_stream_flush(&enc->os, &og)) ) {
- if (!ret) break;
- ret = oe_write_page(&og, &enc->callbacks, enc->user_data);
- if (ret){
- /* FIXME: How do we handle that? */
- assert(0);
- }
- }
-#if 0
- comment_pad(&inopt.comments, &inopt.comments_length, comment_padding);
- op.packet = (unsigned char *)inopt.comments;
- op.bytes = inopt.comments_length;
+ op.packet = (unsigned char *)enc->comment;
+ op.bytes = enc->comment_length;
op.b_o_s = 0;
op.e_o_s = 0;
op.granulepos = 0;
op.packetno = 1;
ogg_stream_packetin(&enc->os, &op);
-#endif
+ oe_flush_page(enc);
}
}