shithub: opus-tools

Download patch

ref: 294847ad2742c94e0f61b56d5a9e02159d91b19e
parent: 2039a8764c77a870d4b403282fb96c74f8775583
author: Mark Harris <mark.hsj@gmail.com>
date: Mon Feb 5 19:02:58 EST 2018

opusenc: Add FLAC pictures using libopusenc

This eliminates the need to do base64 encoding in opusenc, and
ensures that pictures are valid.  Requires a new libopusenc with
ope_comments_add_picture_from_memory().

--- a/src/flac.c
+++ b/src/flac.c
@@ -191,49 +191,28 @@
       }
       break;
     case FLAC__METADATA_TYPE_PICTURE:
-      {
-        char  *buf;
-        char  *b64;
-        size_t mime_type_length;
-        size_t description_length;
-        size_t buf_sz;
-        size_t b64_sz;
-        size_t offs;
-        if(!inopt->copy_pictures)break;
-        mime_type_length=strlen(metadata->data.picture.mime_type);
-        description_length=strlen((char *)metadata->data.picture.description);
-        buf_sz=32+mime_type_length+description_length
-         +metadata->data.picture.data_length;
-        buf=(char *)malloc(buf_sz);
-        offs=0;
-        WRITE_U32_BE(buf+offs,metadata->data.picture.type);
-        offs+=4;
-        WRITE_U32_BE(buf+offs,(FLAC__uint32)mime_type_length);
-        offs+=4;
-        memcpy(buf+offs,metadata->data.picture.mime_type,mime_type_length);
-        offs+=mime_type_length;
-        WRITE_U32_BE(buf+offs,(FLAC__uint32)description_length);
-        offs+=4;
-        memcpy(buf+offs,metadata->data.picture.description,description_length);
-        offs+=description_length;
-        WRITE_U32_BE(buf+offs,metadata->data.picture.width);
-        offs+=4;
-        WRITE_U32_BE(buf+offs,metadata->data.picture.height);
-        offs+=4;
-        WRITE_U32_BE(buf+offs,metadata->data.picture.depth);
-        offs+=4;
-        WRITE_U32_BE(buf+offs,metadata->data.picture.colors);
-        offs+=4;
-        WRITE_U32_BE(buf+offs,metadata->data.picture.data_length);
-        offs+=4;
-        memcpy(buf+offs,metadata->data.picture.data,
-           metadata->data.picture.data_length);
-        b64_sz=BASE64_LENGTH(buf_sz)+1;
-        b64=(char *)malloc(b64_sz);
-        base64_encode(b64,buf,buf_sz);
-        free(buf);
-        ope_comments_add(inopt->comments, "METADATA_BLOCK_PICTURE", b64);
-        free(b64);
+      if(!inopt->copy_pictures)break;
+      if((unsigned)metadata->data.picture.type>20){
+        fprintf(stderr,
+          _("WARNING: Skipping picture with invalid picture type %u\n"),
+          (unsigned)metadata->data.picture.type);
+      }else if(!strcmp(metadata->data.picture.mime_type,"-->")){
+        fprintf(stderr,
+          _("WARNING: Skipping unsupported picture URL (type %u)\n"),
+          (unsigned)metadata->data.picture.type);
+      }else{
+        int ret;
+        ret=ope_comments_add_picture_from_memory(inopt->comments,
+          (const char *)metadata->data.picture.data,
+          (size_t)metadata->data.picture.data_length,
+          (int)metadata->data.picture.type,
+          (const char *)metadata->data.picture.description);
+        if(ret<0){
+          fprintf(stderr,_("WARNING: Skipping picture (%s, type %u): %s\n"),
+            metadata->data.picture.mime_type,
+            (unsigned)metadata->data.picture.type,
+            ope_strerror(ret));
+        }
       }
       break;
     default:
--- a/src/picture.c
+++ b/src/picture.c
@@ -34,51 +34,6 @@
 #include <string.h>
 #include "picture.h"
 
-static const char BASE64_TABLE[64]={
-  'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
-  'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
-  'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
-  'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
-};
-
-/*Utility function for base64 encoding METADATA_BLOCK_PICTURE tags.
-  Stores BASE64_LENGTH(len)+1 bytes in dst (including a terminating NUL).*/
-void base64_encode(char *dst, const char *src, int len){
-  unsigned s0;
-  unsigned s1;
-  unsigned s2;
-  int      ngroups;
-  int      i;
-  ngroups=len/3;
-  for(i=0;i<ngroups;i++){
-    s0=(unsigned char)src[3*i+0];
-    s1=(unsigned char)src[3*i+1];
-    s2=(unsigned char)src[3*i+2];
-    dst[4*i+0]=BASE64_TABLE[s0>>2];
-    dst[4*i+1]=BASE64_TABLE[(s0&3)<<4|s1>>4];
-    dst[4*i+2]=BASE64_TABLE[(s1&15)<<2|s2>>6];
-    dst[4*i+3]=BASE64_TABLE[s2&63];
-  }
-  len-=3*i;
-  if(len==1){
-    s0=(unsigned char)src[3*i+0];
-    dst[4*i+0]=BASE64_TABLE[s0>>2];
-    dst[4*i+1]=BASE64_TABLE[(s0&3)<<4];
-    dst[4*i+2]='=';
-    dst[4*i+3]='=';
-    i++;
-  }
-  else if(len==2){
-    s0=(unsigned char)src[3*i+0];
-    s1=(unsigned char)src[3*i+1];
-    dst[4*i+0]=BASE64_TABLE[s0>>2];
-    dst[4*i+1]=BASE64_TABLE[(s0&3)<<4|s1>>4];
-    dst[4*i+2]=BASE64_TABLE[(s1&15)<<2];
-    dst[4*i+3]='=';
-    i++;
-  }
-  dst[4*i]='\0';
-}
 
 /*A version of strncasecmp() that is guaranteed to only ignore the case of
    ASCII characters.*/
--- a/src/picture.h
+++ b/src/picture.h
@@ -6,12 +6,6 @@
   PIC_FORMAT_GIF
 }picture_format;
 
-#define BASE64_LENGTH(len) (((len)+2)/3*4)
-
-/*Utility function for base64 encoding METADATA_BLOCK_PICTURE tags.
-  Stores BASE64_LENGTH(len)+1 bytes in dst (including a terminating NUL).*/
-void base64_encode(char *dst, const char *src, int len);
-
 int oi_strncasecmp(const char *a, const char *b, int n);
 
 int is_jpeg(const unsigned char *buf, size_t length);
@@ -30,12 +24,3 @@
                          ogg_uint32_t *width, ogg_uint32_t *height,
                          ogg_uint32_t *depth, ogg_uint32_t *colors,
                          int *has_palette);
-
-#define WRITE_U32_BE(buf, val) \
-  do{ \
-    (buf)[0]=(unsigned char)((val)>>24); \
-    (buf)[1]=(unsigned char)((val)>>16); \
-    (buf)[2]=(unsigned char)((val)>>8); \
-    (buf)[3]=(unsigned char)(val); \
-  } \
-  while(0);