| 1 |
/******************************************************************** |
| 2 |
* * |
| 3 |
* THIS FILE IS PART OF THE libopusfile SOFTWARE CODEC SOURCE CODE. * |
| 4 |
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * |
| 5 |
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * |
| 6 |
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * |
| 7 |
* * |
| 8 |
* THE libopusfile SOURCE CODE IS (C) COPYRIGHT 2012-2020 * |
| 9 |
* by the Xiph.Org Foundation and contributors https://xiph.org/ * |
| 10 |
* * |
| 11 |
********************************************************************/ |
| 12 |
//#ifdef HAVE_CONFIG_H |
| 13 |
#include "opus__config.h" |
| 14 |
//#endif |
| 15 |
|
| 16 |
#include "opusfile_internal.h" |
| 17 |
#include <limits.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
static unsigned op_parse_uint16le(const unsigned char *_data){ |
| 21 |
return _data[0]|_data[1]<<8; |
| 22 |
} |
| 23 |
|
| 24 |
static int op_parse_int16le(const unsigned char *_data){ |
| 25 |
int ret; |
| 26 |
ret=_data[0]|_data[1]<<8; |
| 27 |
return (ret^0x8000)-0x8000; |
| 28 |
} |
| 29 |
|
| 30 |
static opus_uint32 op_parse_uint32le(const unsigned char *_data){ |
| 31 |
return _data[0]|(opus_uint32)_data[1]<<8| |
| 32 |
(opus_uint32)_data[2]<<16|(opus_uint32)_data[3]<<24; |
| 33 |
} |
| 34 |
|
| 35 |
static opus_uint32 op_parse_uint32be(const unsigned char *_data){ |
| 36 |
return _data[3]|(opus_uint32)_data[2]<<8| |
| 37 |
(opus_uint32)_data[1]<<16|(opus_uint32)_data[0]<<24; |
| 38 |
} |
| 39 |
|
| 40 |
int opus_head_parse(OpusHead *_head,const unsigned char *_data,size_t _len){ |
| 41 |
OpusHead head; |
| 42 |
if(_len<8)return OP_ENOTFORMAT; |
| 43 |
if(memcmp(_data,"OpusHead",8)!=0)return OP_ENOTFORMAT; |
| 44 |
if(_len<9)return OP_EBADHEADER; |
| 45 |
head.version=_data[8]; |
| 46 |
if(head.version>15)return OP_EVERSION; |
| 47 |
if(_len<19)return OP_EBADHEADER; |
| 48 |
head.channel_count=_data[9]; |
| 49 |
head.pre_skip=op_parse_uint16le(_data+10); |
| 50 |
head.input_sample_rate=op_parse_uint32le(_data+12); |
| 51 |
head.output_gain=op_parse_int16le(_data+16); |
| 52 |
head.mapping_family=_data[18]; |
| 53 |
if(head.mapping_family==0){ |
| 54 |
if(head.channel_count<1||head.channel_count>2)return OP_EBADHEADER; |
| 55 |
if(head.version<=1&&_len>19)return OP_EBADHEADER; |
| 56 |
head.stream_count=1; |
| 57 |
head.coupled_count=head.channel_count-1; |
| 58 |
if(_head!=NULL){ |
| 59 |
_head->mapping[0]=0; |
| 60 |
_head->mapping[1]=1; |
| 61 |
} |
| 62 |
} |
| 63 |
else if(head.mapping_family==1){ |
| 64 |
size_t size; |
| 65 |
int ci; |
| 66 |
if(head.channel_count<1||head.channel_count>8)return OP_EBADHEADER; |
| 67 |
size=21+head.channel_count; |
| 68 |
if(_len<size||head.version<=1&&_len>size)return OP_EBADHEADER; |
| 69 |
head.stream_count=_data[19]; |
| 70 |
if(head.stream_count<1)return OP_EBADHEADER; |
| 71 |
head.coupled_count=_data[20]; |
| 72 |
if(head.coupled_count>head.stream_count)return OP_EBADHEADER; |
| 73 |
for(ci=0;ci<head.channel_count;ci++){ |
| 74 |
if(_data[21+ci]>=head.stream_count+head.coupled_count |
| 75 |
&&_data[21+ci]!=255){ |
| 76 |
return OP_EBADHEADER; |
| 77 |
} |
| 78 |
} |
| 79 |
if(_head!=NULL)memcpy(_head->mapping,_data+21,head.channel_count); |
| 80 |
} |
| 81 |
/*General purpose players should not attempt to play back content with |
| 82 |
channel mapping family 255.*/ |
| 83 |
else if(head.mapping_family==255)return OP_EIMPL; |
| 84 |
/*No other channel mapping families are currently defined.*/ |
| 85 |
else return OP_EBADHEADER; |
| 86 |
if(_head!=NULL)memcpy(_head,&head,head.mapping-(unsigned char *)&head); |
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
void opus_tags_init(OpusTags *_tags){ |
| 91 |
memset(_tags,0,sizeof(*_tags)); |
| 92 |
} |
| 93 |
|
| 94 |
void opus_tags_clear(OpusTags *_tags){ |
| 95 |
int ncomments; |
| 96 |
int ci; |
| 97 |
ncomments=_tags->comments; |
| 98 |
if(_tags->user_comments!=NULL)ncomments++; |
| 99 |
else{ |
| 100 |
OP_ASSERT(ncomments==0); |
| 101 |
} |
| 102 |
for(ci=ncomments;ci-->0;)Melder_free(_tags->user_comments[ci]); |
| 103 |
Melder_free(_tags->user_comments); |
| 104 |
Melder_free(_tags->comment_lengths); |
| 105 |
Melder_free(_tags->vendor); |
| 106 |
} |
| 107 |
|
| 108 |
/*Ensure there's room for up to _ncomments comments.*/ |
| 109 |
static int op_tags_ensure_capacity(OpusTags *_tags,size_t _ncomments){ |
| 110 |
char **user_comments; |
| 111 |
int *comment_lengths; |
| 112 |
int cur_ncomments; |
| 113 |
size_t size; |
| 114 |
if(OP_UNLIKELY(_ncomments>=(size_t)INT_MAX))return OP_EFAULT; |
| 115 |
size=sizeof(*_tags->comment_lengths)*(_ncomments+1); |
| 116 |
if(size/sizeof(*_tags->comment_lengths)!=_ncomments+1)return OP_EFAULT; |
| 117 |
cur_ncomments=_tags->comments; |
| 118 |
/*We only support growing. |
| 119 |
Trimming requires cleaning up the allocated strings in the old space, and |
| 120 |
is best handled separately if it's ever needed.*/ |
| 121 |
OP_ASSERT(_ncomments>=(size_t)cur_ncomments); |
| 122 |
comment_lengths=(int *)Melder_realloc(_tags->comment_lengths,size); |
| 123 |
if(OP_UNLIKELY(comment_lengths==NULL))return OP_EFAULT; |
| 124 |
if(_tags->comment_lengths==NULL){ |
| 125 |
OP_ASSERT(cur_ncomments==0); |
| 126 |
comment_lengths[cur_ncomments]=0; |
| 127 |
} |
| 128 |
comment_lengths[_ncomments]=comment_lengths[cur_ncomments]; |
| 129 |
_tags->comment_lengths=comment_lengths; |
| 130 |
size=sizeof(*_tags->user_comments)*(_ncomments+1); |
| 131 |
if(size/sizeof(*_tags->user_comments)!=_ncomments+1)return OP_EFAULT; |
| 132 |
user_comments=(char **)Melder_realloc(_tags->user_comments,size); |
| 133 |
if(OP_UNLIKELY(user_comments==NULL))return OP_EFAULT; |
| 134 |
if(_tags->user_comments==NULL){ |
| 135 |
OP_ASSERT(cur_ncomments==0); |
| 136 |
user_comments[cur_ncomments]=NULL; |
| 137 |
} |
| 138 |
user_comments[_ncomments]=user_comments[cur_ncomments]; |
| 139 |
_tags->user_comments=user_comments; |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
/*Duplicate a (possibly non-NUL terminated) string with a known length.*/ |
| 144 |
static char *op_strdup_with_len(const char *_s,size_t _len){ |
| 145 |
size_t size; |
| 146 |
char *ret; |
| 147 |
size=sizeof(*ret)*(_len+1); |
| 148 |
if(OP_UNLIKELY(size<_len))return NULL; |
| 149 |
ret=(char *)_Melder_malloc(size); |
| 150 |
if(OP_LIKELY(ret!=NULL)){ |
| 151 |
ret=(char *)memcpy(ret,_s,sizeof(*ret)*_len); |
| 152 |
ret[_len]='\0'; |
| 153 |
} |
| 154 |
return ret; |
| 155 |
} |
| 156 |
|
| 157 |
/*The actual implementation of opus_tags_parse(). |
| 158 |
Unlike the public API, this function requires _tags to already be |
| 159 |
initialized, modifies its contents before success is guaranteed, and assumes |
| 160 |
the caller will clear it on error.*/ |
| 161 |
static int opus_tags_parse_impl(OpusTags *_tags, |
| 162 |
const unsigned char *_data,size_t _len){ |
| 163 |
opus_uint32 count; |
| 164 |
size_t len; |
| 165 |
int ncomments; |
| 166 |
int ci; |
| 167 |
len=_len; |
| 168 |
if(len<8)return OP_ENOTFORMAT; |
| 169 |
if(memcmp(_data,"OpusTags",8)!=0)return OP_ENOTFORMAT; |
| 170 |
if(len<16)return OP_EBADHEADER; |
| 171 |
_data+=8; |
| 172 |
len-=8; |
| 173 |
count=op_parse_uint32le(_data); |
| 174 |
_data+=4; |
| 175 |
len-=4; |
| 176 |
if(count>len)return OP_EBADHEADER; |
| 177 |
if(_tags!=NULL){ |
| 178 |
_tags->vendor=op_strdup_with_len((char *)_data,count); |
| 179 |
if(_tags->vendor==NULL)return OP_EFAULT; |
| 180 |
} |
| 181 |
_data+=count; |
| 182 |
len-=count; |
| 183 |
if(len<4)return OP_EBADHEADER; |
| 184 |
count=op_parse_uint32le(_data); |
| 185 |
_data+=4; |
| 186 |
len-=4; |
| 187 |
/*Check to make sure there's minimally sufficient data left in the packet.*/ |
| 188 |
if(count>len>>2)return OP_EBADHEADER; |
| 189 |
/*Check for overflow (the API limits this to an int).*/ |
| 190 |
if(count>(opus_uint32)INT_MAX-1)return OP_EFAULT; |
| 191 |
if(_tags!=NULL){ |
| 192 |
int ret; |
| 193 |
ret=op_tags_ensure_capacity(_tags,count); |
| 194 |
if(ret<0)return ret; |
| 195 |
} |
| 196 |
ncomments=(int)count; |
| 197 |
for(ci=0;ci<ncomments;ci++){ |
| 198 |
/*Check to make sure there's minimally sufficient data left in the packet.*/ |
| 199 |
if((size_t)(ncomments-ci)>len>>2)return OP_EBADHEADER; |
| 200 |
count=op_parse_uint32le(_data); |
| 201 |
_data+=4; |
| 202 |
len-=4; |
| 203 |
if(count>len)return OP_EBADHEADER; |
| 204 |
/*Check for overflow (the API limits this to an int).*/ |
| 205 |
if(count>(opus_uint32)INT_MAX)return OP_EFAULT; |
| 206 |
if(_tags!=NULL){ |
| 207 |
_tags->user_comments[ci]=op_strdup_with_len((char *)_data,count); |
| 208 |
if(_tags->user_comments[ci]==NULL)return OP_EFAULT; |
| 209 |
_tags->comment_lengths[ci]=(int)count; |
| 210 |
_tags->comments=ci+1; |
| 211 |
/*Needed by opus_tags_clear() if we fail before parsing the (optional) |
| 212 |
binary metadata.*/ |
| 213 |
_tags->user_comments[ci+1]=NULL; |
| 214 |
} |
| 215 |
_data+=count; |
| 216 |
len-=count; |
| 217 |
} |
| 218 |
if(len>0&&(_data[0]&1)){ |
| 219 |
if(len>(opus_uint32)INT_MAX)return OP_EFAULT; |
| 220 |
if(_tags!=NULL){ |
| 221 |
_tags->user_comments[ncomments]=(char *)_Melder_malloc(len); |
| 222 |
if(OP_UNLIKELY(_tags->user_comments[ncomments]==NULL))return OP_EFAULT; |
| 223 |
memcpy(_tags->user_comments[ncomments],_data,len); |
| 224 |
_tags->comment_lengths[ncomments]=(int)len; |
| 225 |
} |
| 226 |
} |
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
int opus_tags_parse(OpusTags *_tags,const unsigned char *_data,size_t _len){ |
| 231 |
if(_tags!=NULL){ |
| 232 |
OpusTags tags; |
| 233 |
int ret; |
| 234 |
opus_tags_init(&tags); |
| 235 |
ret=opus_tags_parse_impl(&tags,_data,_len); |
| 236 |
if(ret<0)opus_tags_clear(&tags); |
| 237 |
else *_tags=*&tags; |
| 238 |
return ret; |
| 239 |
} |
| 240 |
else return opus_tags_parse_impl(NULL,_data,_len); |
| 241 |
} |
| 242 |
|
| 243 |
/*The actual implementation of opus_tags_copy(). |
| 244 |
Unlike the public API, this function requires _dst to already be |
| 245 |
initialized, modifies its contents before success is guaranteed, and assumes |
| 246 |
the caller will clear it on error.*/ |
| 247 |
static int opus_tags_copy_impl(OpusTags *_dst,const OpusTags *_src){ |
| 248 |
char *vendor; |
| 249 |
int ncomments; |
| 250 |
int ret; |
| 251 |
int ci; |
| 252 |
vendor=_src->vendor; |
| 253 |
_dst->vendor=op_strdup_with_len(vendor,strlen(vendor)); |
| 254 |
if(OP_UNLIKELY(_dst->vendor==NULL))return OP_EFAULT; |
| 255 |
ncomments=_src->comments; |
| 256 |
ret=op_tags_ensure_capacity(_dst,ncomments); |
| 257 |
if(OP_UNLIKELY(ret<0))return ret; |
| 258 |
for(ci=0;ci<ncomments;ci++){ |
| 259 |
int len; |
| 260 |
len=_src->comment_lengths[ci]; |
| 261 |
OP_ASSERT(len>=0); |
| 262 |
_dst->user_comments[ci]=op_strdup_with_len(_src->user_comments[ci],len); |
| 263 |
if(OP_UNLIKELY(_dst->user_comments[ci]==NULL))return OP_EFAULT; |
| 264 |
_dst->comment_lengths[ci]=len; |
| 265 |
_dst->comments=ci+1; |
| 266 |
} |
| 267 |
if(_src->comment_lengths!=NULL){ |
| 268 |
int len; |
| 269 |
len=_src->comment_lengths[ncomments]; |
| 270 |
if(len>0){ |
| 271 |
_dst->user_comments[ncomments]=(char *)_Melder_malloc(len); |
| 272 |
if(OP_UNLIKELY(_dst->user_comments[ncomments]==NULL))return OP_EFAULT; |
| 273 |
memcpy(_dst->user_comments[ncomments],_src->user_comments[ncomments],len); |
| 274 |
_dst->comment_lengths[ncomments]=len; |
| 275 |
} |
| 276 |
} |
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
int opus_tags_copy(OpusTags *_dst,const OpusTags *_src){ |
| 281 |
OpusTags dst; |
| 282 |
int ret; |
| 283 |
opus_tags_init(&dst); |
| 284 |
ret=opus_tags_copy_impl(&dst,_src); |
| 285 |
if(OP_UNLIKELY(ret<0))opus_tags_clear(&dst); |
| 286 |
else *_dst=*&dst; |
| 287 |
return ret; |
| 288 |
} |
| 289 |
|
| 290 |
int opus_tags_add(OpusTags *_tags,const char *_tag,const char *_value){ |
| 291 |
char *comment; |
| 292 |
size_t tag_len; |
| 293 |
size_t value_len; |
| 294 |
int ncomments; |
| 295 |
int ret; |
| 296 |
ncomments=_tags->comments; |
| 297 |
ret=op_tags_ensure_capacity(_tags,ncomments+1); |
| 298 |
if(OP_UNLIKELY(ret<0))return ret; |
| 299 |
tag_len=strlen(_tag); |
| 300 |
value_len=strlen(_value); |
| 301 |
/*+2 for '=' and '\0'.*/ |
| 302 |
if(tag_len+value_len<tag_len)return OP_EFAULT; |
| 303 |
if(tag_len+value_len>(size_t)INT_MAX-2)return OP_EFAULT; |
| 304 |
comment=(char *)_Melder_malloc(sizeof(*comment)*(tag_len+value_len+2)); |
| 305 |
if(OP_UNLIKELY(comment==NULL))return OP_EFAULT; |
| 306 |
memcpy(comment,_tag,sizeof(*comment)*tag_len); |
| 307 |
comment[tag_len]='='; |
| 308 |
memcpy(comment+tag_len+1,_value,sizeof(*comment)*(value_len+1)); |
| 309 |
_tags->user_comments[ncomments]=comment; |
| 310 |
_tags->comment_lengths[ncomments]=(int)(tag_len+value_len+1); |
| 311 |
_tags->comments=ncomments+1; |
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
int opus_tags_add_comment(OpusTags *_tags,const char *_comment){ |
| 316 |
char *comment; |
| 317 |
int comment_len; |
| 318 |
int ncomments; |
| 319 |
int ret; |
| 320 |
ncomments=_tags->comments; |
| 321 |
ret=op_tags_ensure_capacity(_tags,ncomments+1); |
| 322 |
if(OP_UNLIKELY(ret<0))return ret; |
| 323 |
comment_len=(int)strlen(_comment); |
| 324 |
comment=op_strdup_with_len(_comment,comment_len); |
| 325 |
if(OP_UNLIKELY(comment==NULL))return OP_EFAULT; |
| 326 |
_tags->user_comments[ncomments]=comment; |
| 327 |
_tags->comment_lengths[ncomments]=comment_len; |
| 328 |
_tags->comments=ncomments+1; |
| 329 |
return 0; |
| 330 |
} |
| 331 |
|
| 332 |
int opus_tags_set_binary_suffix(OpusTags *_tags, |
| 333 |
const unsigned char *_data,int _len){ |
| 334 |
unsigned char *binary_suffix_data; |
| 335 |
int ncomments; |
| 336 |
int ret; |
| 337 |
if(_len<0||_len>0&&(_data==NULL||!(_data[0]&1)))return OP_EINVAL; |
| 338 |
ncomments=_tags->comments; |
| 339 |
ret=op_tags_ensure_capacity(_tags,ncomments); |
| 340 |
if(OP_UNLIKELY(ret<0))return ret; |
| 341 |
binary_suffix_data= |
| 342 |
(unsigned char *)Melder_realloc(_tags->user_comments[ncomments],_len); |
| 343 |
if(OP_UNLIKELY(binary_suffix_data==NULL))return OP_EFAULT; |
| 344 |
memcpy(binary_suffix_data,_data,_len); |
| 345 |
_tags->user_comments[ncomments]=(char *)binary_suffix_data; |
| 346 |
_tags->comment_lengths[ncomments]=_len; |
| 347 |
return 0; |
| 348 |
} |
| 349 |
|
| 350 |
int opus_tagcompare(const char *_tag_name,const char *_comment){ |
| 351 |
size_t tag_len; |
| 352 |
tag_len=strlen(_tag_name); |
| 353 |
if(OP_UNLIKELY(tag_len>(size_t)INT_MAX))return -1; |
| 354 |
return opus_tagncompare(_tag_name,(int)tag_len,_comment); |
| 355 |
} |
| 356 |
|
| 357 |
int opus_tagncompare(const char *_tag_name,int _tag_len,const char *_comment){ |
| 358 |
int ret; |
| 359 |
OP_ASSERT(_tag_len>=0); |
| 360 |
ret=op_strncasecmp(_tag_name,_comment,_tag_len); |
| 361 |
return ret?ret:'='-_comment[_tag_len]; |
| 362 |
} |
| 363 |
|
| 364 |
const char *opus_tags_query(const OpusTags *_tags,const char *_tag,int _count){ |
| 365 |
char **user_comments; |
| 366 |
size_t tag_len; |
| 367 |
int found; |
| 368 |
int ncomments; |
| 369 |
int ci; |
| 370 |
tag_len=strlen(_tag); |
| 371 |
if(OP_UNLIKELY(tag_len>(size_t)INT_MAX))return NULL; |
| 372 |
ncomments=_tags->comments; |
| 373 |
user_comments=_tags->user_comments; |
| 374 |
found=0; |
| 375 |
for(ci=0;ci<ncomments;ci++){ |
| 376 |
if(!opus_tagncompare(_tag,(int)tag_len,user_comments[ci])){ |
| 377 |
/*We return a pointer to the data, not a copy.*/ |
| 378 |
if(_count==found++)return user_comments[ci]+tag_len+1; |
| 379 |
} |
| 380 |
} |
| 381 |
/*Didn't find anything.*/ |
| 382 |
return NULL; |
| 383 |
} |
| 384 |
|
| 385 |
int opus_tags_query_count(const OpusTags *_tags,const char *_tag){ |
| 386 |
char **user_comments; |
| 387 |
size_t tag_len; |
| 388 |
int found; |
| 389 |
int ncomments; |
| 390 |
int ci; |
| 391 |
tag_len=strlen(_tag); |
| 392 |
if(OP_UNLIKELY(tag_len>(size_t)INT_MAX))return 0; |
| 393 |
ncomments=_tags->comments; |
| 394 |
user_comments=_tags->user_comments; |
| 395 |
found=0; |
| 396 |
for(ci=0;ci<ncomments;ci++){ |
| 397 |
if(!opus_tagncompare(_tag,(int)tag_len,user_comments[ci]))found++; |
| 398 |
} |
| 399 |
return found; |
| 400 |
} |
| 401 |
|
| 402 |
const unsigned char *opus_tags_get_binary_suffix(const OpusTags *_tags, |
| 403 |
int *_len){ |
| 404 |
int ncomments; |
| 405 |
int len; |
| 406 |
ncomments=_tags->comments; |
| 407 |
len=_tags->comment_lengths==NULL?0:_tags->comment_lengths[ncomments]; |
| 408 |
*_len=len; |
| 409 |
OP_ASSERT(len==0||_tags->user_comments!=NULL); |
| 410 |
return len>0?(const unsigned char *)_tags->user_comments[ncomments]:NULL; |
| 411 |
} |
| 412 |
|
| 413 |
static int opus_tags_get_gain(const OpusTags *_tags,int *_gain_q8, |
| 414 |
const char *_tag_name,size_t _tag_len){ |
| 415 |
char **comments; |
| 416 |
int ncomments; |
| 417 |
int ci; |
| 418 |
comments=_tags->user_comments; |
| 419 |
ncomments=_tags->comments; |
| 420 |
/*Look for the first valid tag with the name _tag_name and use that.*/ |
| 421 |
for(ci=0;ci<ncomments;ci++){ |
| 422 |
OP_ASSERT(_tag_len<=(size_t)INT_MAX); |
| 423 |
if(opus_tagncompare(_tag_name,(int)_tag_len,comments[ci])==0){ |
| 424 |
char *p; |
| 425 |
opus_int32 gain_q8; |
| 426 |
int negative; |
| 427 |
p=comments[ci]+_tag_len+1; |
| 428 |
negative=0; |
| 429 |
if(*p=='-'){ |
| 430 |
negative=-1; |
| 431 |
p++; |
| 432 |
} |
| 433 |
else if(*p=='+')p++; |
| 434 |
gain_q8=0; |
| 435 |
while(*p>='0'&&*p<='9'){ |
| 436 |
gain_q8=10*gain_q8+*p-'0'; |
| 437 |
if(gain_q8>32767-negative)break; |
| 438 |
p++; |
| 439 |
} |
| 440 |
/*This didn't look like a signed 16-bit decimal integer. |
| 441 |
Not a valid gain tag.*/ |
| 442 |
if(*p!='\0')continue; |
| 443 |
*_gain_q8=(int)(gain_q8+negative^negative); |
| 444 |
return 0; |
| 445 |
} |
| 446 |
} |
| 447 |
return OP_FALSE; |
| 448 |
} |
| 449 |
|
| 450 |
int opus_tags_get_album_gain(const OpusTags *_tags,int *_gain_q8){ |
| 451 |
return opus_tags_get_gain(_tags,_gain_q8,"R128_ALBUM_GAIN",15); |
| 452 |
} |
| 453 |
|
| 454 |
int opus_tags_get_track_gain(const OpusTags *_tags,int *_gain_q8){ |
| 455 |
return opus_tags_get_gain(_tags,_gain_q8,"R128_TRACK_GAIN",15); |
| 456 |
} |
| 457 |
|
| 458 |
static int op_is_jpeg(const unsigned char *_buf,size_t _buf_sz){ |
| 459 |
return _buf_sz>=3&&memcmp(_buf,"\xFF\xD8\xFF",3)==0; |
| 460 |
} |
| 461 |
|
| 462 |
/*Tries to extract the width, height, bits per pixel, and palette size of a |
| 463 |
JPEG. |
| 464 |
On failure, simply leaves its outputs unmodified.*/ |
| 465 |
static void op_extract_jpeg_params(const unsigned char *_buf,size_t _buf_sz, |
| 466 |
opus_uint32 *_width,opus_uint32 *_height, |
| 467 |
opus_uint32 *_depth,opus_uint32 *_colors,int *_has_palette){ |
| 468 |
if(op_is_jpeg(_buf,_buf_sz)){ |
| 469 |
size_t offs; |
| 470 |
offs=2; |
| 471 |
for(;;){ |
| 472 |
size_t segment_len; |
| 473 |
int marker; |
| 474 |
while(offs<_buf_sz&&_buf[offs]!=0xFF)offs++; |
| 475 |
while(offs<_buf_sz&&_buf[offs]==0xFF)offs++; |
| 476 |
marker=_buf[offs]; |
| 477 |
offs++; |
| 478 |
/*If we hit EOI* (end of image), or another SOI* (start of image), |
| 479 |
or SOS (start of scan), then stop now.*/ |
| 480 |
if(offs>=_buf_sz||(marker>=0xD8&&marker<=0xDA))break; |
| 481 |
/*RST* (restart markers): skip (no segment length).*/ |
| 482 |
else if(marker>=0xD0&&marker<=0xD7)continue; |
| 483 |
/*Read the length of the marker segment.*/ |
| 484 |
if(_buf_sz-offs<2)break; |
| 485 |
segment_len=_buf[offs]<<8|_buf[offs+1]; |
| 486 |
if(segment_len<2||_buf_sz-offs<segment_len)break; |
| 487 |
if(marker==0xC0||(marker>0xC0&&marker<0xD0&&(marker&3)!=0)){ |
| 488 |
/*Found a SOFn (start of frame) marker segment:*/ |
| 489 |
if(segment_len>=8){ |
| 490 |
*_height=_buf[offs+3]<<8|_buf[offs+4]; |
| 491 |
*_width=_buf[offs+5]<<8|_buf[offs+6]; |
| 492 |
*_depth=_buf[offs+2]*_buf[offs+7]; |
| 493 |
*_colors=0; |
| 494 |
*_has_palette=0; |
| 495 |
} |
| 496 |
break; |
| 497 |
} |
| 498 |
/*Other markers: skip the whole marker segment.*/ |
| 499 |
offs+=segment_len; |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
static int op_is_png(const unsigned char *_buf,size_t _buf_sz){ |
| 505 |
return _buf_sz>=8&&memcmp(_buf,"\x89PNG\x0D\x0A\x1A\x0A",8)==0; |
| 506 |
} |
| 507 |
|
| 508 |
/*Tries to extract the width, height, bits per pixel, and palette size of a |
| 509 |
PNG. |
| 510 |
On failure, simply leaves its outputs unmodified.*/ |
| 511 |
static void op_extract_png_params(const unsigned char *_buf,size_t _buf_sz, |
| 512 |
opus_uint32 *_width,opus_uint32 *_height, |
| 513 |
opus_uint32 *_depth,opus_uint32 *_colors,int *_has_palette){ |
| 514 |
if(op_is_png(_buf,_buf_sz)){ |
| 515 |
size_t offs; |
| 516 |
offs=8; |
| 517 |
while(_buf_sz-offs>=12){ |
| 518 |
ogg_uint32_t chunk_len; |
| 519 |
chunk_len=op_parse_uint32be(_buf+offs); |
| 520 |
if(chunk_len>_buf_sz-(offs+12))break; |
| 521 |
else if(chunk_len==13&&memcmp(_buf+offs+4,"IHDR",4)==0){ |
| 522 |
int color_type; |
| 523 |
*_width=op_parse_uint32be(_buf+offs+8); |
| 524 |
*_height=op_parse_uint32be(_buf+offs+12); |
| 525 |
color_type=_buf[offs+17]; |
| 526 |
if(color_type==3){ |
| 527 |
*_depth=24; |
| 528 |
*_has_palette=1; |
| 529 |
} |
| 530 |
else{ |
| 531 |
int sample_depth; |
| 532 |
sample_depth=_buf[offs+16]; |
| 533 |
if(color_type==0)*_depth=sample_depth; |
| 534 |
else if(color_type==2)*_depth=sample_depth*3; |
| 535 |
else if(color_type==4)*_depth=sample_depth*2; |
| 536 |
else if(color_type==6)*_depth=sample_depth*4; |
| 537 |
*_colors=0; |
| 538 |
*_has_palette=0; |
| 539 |
break; |
| 540 |
} |
| 541 |
} |
| 542 |
else if(*_has_palette>0&&memcmp(_buf+offs+4,"PLTE",4)==0){ |
| 543 |
*_colors=chunk_len/3; |
| 544 |
break; |
| 545 |
} |
| 546 |
offs+=12+chunk_len; |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
static int op_is_gif(const unsigned char *_buf,size_t _buf_sz){ |
| 552 |
return _buf_sz>=6&&(memcmp(_buf,"GIF87a",6)==0||memcmp(_buf,"GIF89a",6)==0); |
| 553 |
} |
| 554 |
|
| 555 |
/*Tries to extract the width, height, bits per pixel, and palette size of a |
| 556 |
GIF. |
| 557 |
On failure, simply leaves its outputs unmodified.*/ |
| 558 |
static void op_extract_gif_params(const unsigned char *_buf,size_t _buf_sz, |
| 559 |
opus_uint32 *_width,opus_uint32 *_height, |
| 560 |
opus_uint32 *_depth,opus_uint32 *_colors,int *_has_palette){ |
| 561 |
if(op_is_gif(_buf,_buf_sz)&&_buf_sz>=14){ |
| 562 |
*_width=_buf[6]|_buf[7]<<8; |
| 563 |
*_height=_buf[8]|_buf[9]<<8; |
| 564 |
/*libFLAC hard-codes the depth to 24.*/ |
| 565 |
*_depth=24; |
| 566 |
*_colors=1<<((_buf[10]&7)+1); |
| 567 |
*_has_palette=1; |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
/*The actual implementation of opus_picture_tag_parse(). |
| 572 |
Unlike the public API, this function requires _pic to already be |
| 573 |
initialized, modifies its contents before success is guaranteed, and assumes |
| 574 |
the caller will clear it on error.*/ |
| 575 |
static int opus_picture_tag_parse_impl(OpusPictureTag *_pic,const char *_tag, |
| 576 |
unsigned char *_buf,size_t _buf_sz,size_t _base64_sz){ |
| 577 |
opus_int32 picture_type; |
| 578 |
opus_uint32 mime_type_length; |
| 579 |
char *mime_type; |
| 580 |
opus_uint32 description_length; |
| 581 |
char *description; |
| 582 |
opus_uint32 width; |
| 583 |
opus_uint32 height; |
| 584 |
opus_uint32 depth; |
| 585 |
opus_uint32 colors; |
| 586 |
opus_uint32 data_length; |
| 587 |
opus_uint32 file_width; |
| 588 |
opus_uint32 file_height; |
| 589 |
opus_uint32 file_depth; |
| 590 |
opus_uint32 file_colors; |
| 591 |
int format; |
| 592 |
int has_palette; |
| 593 |
int colors_set; |
| 594 |
size_t i; |
| 595 |
/*Decode the BASE64 data.*/ |
| 596 |
OP_ASSERT(_base64_sz>=11); |
| 597 |
for(i=0;i<_base64_sz;i++){ |
| 598 |
opus_uint32 value; |
| 599 |
int j; |
| 600 |
value=0; |
| 601 |
for(j=0;j<4;j++){ |
| 602 |
unsigned c; |
| 603 |
unsigned d; |
| 604 |
c=(unsigned char)_tag[4*i+j]; |
| 605 |
if(c=='+')d=62; |
| 606 |
else if(c=='/')d=63; |
| 607 |
else if(c>='0'&&c<='9')d=52+c-'0'; |
| 608 |
else if(c>='a'&&c<='z')d=26+c-'a'; |
| 609 |
else if(c>='A'&&c<='Z')d=c-'A'; |
| 610 |
else if(c=='='&&3*i+j>_buf_sz)d=0; |
| 611 |
else return OP_ENOTFORMAT; |
| 612 |
value=value<<6|d; |
| 613 |
} |
| 614 |
_buf[3*i]=(unsigned char)(value>>16); |
| 615 |
if(3*i+1<_buf_sz){ |
| 616 |
_buf[3*i+1]=(unsigned char)(value>>8); |
| 617 |
if(3*i+2<_buf_sz)_buf[3*i+2]=(unsigned char)value; |
| 618 |
} |
| 619 |
} |
| 620 |
i=0; |
| 621 |
picture_type=op_parse_uint32be(_buf+i); |
| 622 |
i+=4; |
| 623 |
/*Extract the MIME type.*/ |
| 624 |
mime_type_length=op_parse_uint32be(_buf+i); |
| 625 |
i+=4; |
| 626 |
if(mime_type_length>_buf_sz-32)return OP_ENOTFORMAT; |
| 627 |
mime_type=(char *)_Melder_malloc(sizeof(*_pic->mime_type)*(mime_type_length+1)); |
| 628 |
if(mime_type==NULL)return OP_EFAULT; |
| 629 |
memcpy(mime_type,_buf+i,sizeof(*mime_type)*mime_type_length); |
| 630 |
mime_type[mime_type_length]='\0'; |
| 631 |
_pic->mime_type=mime_type; |
| 632 |
i+=mime_type_length; |
| 633 |
/*Extract the description string.*/ |
| 634 |
description_length=op_parse_uint32be(_buf+i); |
| 635 |
i+=4; |
| 636 |
if(description_length>_buf_sz-mime_type_length-32)return OP_ENOTFORMAT; |
| 637 |
description= |
| 638 |
(char *)_Melder_malloc(sizeof(*_pic->mime_type)*(description_length+1)); |
| 639 |
if(description==NULL)return OP_EFAULT; |
| 640 |
memcpy(description,_buf+i,sizeof(*description)*description_length); |
| 641 |
description[description_length]='\0'; |
| 642 |
_pic->description=description; |
| 643 |
i+=description_length; |
| 644 |
/*Extract the remaining fields.*/ |
| 645 |
width=op_parse_uint32be(_buf+i); |
| 646 |
i+=4; |
| 647 |
height=op_parse_uint32be(_buf+i); |
| 648 |
i+=4; |
| 649 |
depth=op_parse_uint32be(_buf+i); |
| 650 |
i+=4; |
| 651 |
colors=op_parse_uint32be(_buf+i); |
| 652 |
i+=4; |
| 653 |
/*If one of these is set, they all must be, but colors==0 is a valid value.*/ |
| 654 |
colors_set=width!=0||height!=0||depth!=0||colors!=0; |
| 655 |
if((width==0||height==0||depth==0)&&colors_set)return OP_ENOTFORMAT; |
| 656 |
data_length=op_parse_uint32be(_buf+i); |
| 657 |
i+=4; |
| 658 |
if(data_length>_buf_sz-i)return OP_ENOTFORMAT; |
| 659 |
/*Trim extraneous data so we don't copy it below.*/ |
| 660 |
_buf_sz=i+data_length; |
| 661 |
/*Attempt to determine the image format.*/ |
| 662 |
format=OP_PIC_FORMAT_UNKNOWN; |
| 663 |
if(mime_type_length==3&&strcmp(mime_type,"-->")==0){ |
| 664 |
format=OP_PIC_FORMAT_URL; |
| 665 |
/*Picture type 1 must be a 32x32 PNG.*/ |
| 666 |
if(picture_type==1&&(width!=0||height!=0)&&(width!=32||height!=32)){ |
| 667 |
return OP_ENOTFORMAT; |
| 668 |
} |
| 669 |
/*Append a terminating NUL for the convenience of our callers.*/ |
| 670 |
_buf[_buf_sz++]='\0'; |
| 671 |
} |
| 672 |
else{ |
| 673 |
if(mime_type_length==10 |
| 674 |
&&op_strncasecmp(mime_type,"image/jpeg",mime_type_length)==0){ |
| 675 |
if(op_is_jpeg(_buf+i,data_length))format=OP_PIC_FORMAT_JPEG; |
| 676 |
} |
| 677 |
else if(mime_type_length==9 |
| 678 |
&&op_strncasecmp(mime_type,"image/png",mime_type_length)==0){ |
| 679 |
if(op_is_png(_buf+i,data_length))format=OP_PIC_FORMAT_PNG; |
| 680 |
} |
| 681 |
else if(mime_type_length==9 |
| 682 |
&&op_strncasecmp(mime_type,"image/gif",mime_type_length)==0){ |
| 683 |
if(op_is_gif(_buf+i,data_length))format=OP_PIC_FORMAT_GIF; |
| 684 |
} |
| 685 |
else if(mime_type_length==0||(mime_type_length==6 |
| 686 |
&&op_strncasecmp(mime_type,"image/",mime_type_length)==0)){ |
| 687 |
if(op_is_jpeg(_buf+i,data_length))format=OP_PIC_FORMAT_JPEG; |
| 688 |
else if(op_is_png(_buf+i,data_length))format=OP_PIC_FORMAT_PNG; |
| 689 |
else if(op_is_gif(_buf+i,data_length))format=OP_PIC_FORMAT_GIF; |
| 690 |
} |
| 691 |
file_width=file_height=file_depth=file_colors=0; |
| 692 |
has_palette=-1; |
| 693 |
switch(format){ |
| 694 |
case OP_PIC_FORMAT_JPEG:{ |
| 695 |
op_extract_jpeg_params(_buf+i,data_length, |
| 696 |
&file_width,&file_height,&file_depth,&file_colors,&has_palette); |
| 697 |
}break; |
| 698 |
case OP_PIC_FORMAT_PNG:{ |
| 699 |
op_extract_png_params(_buf+i,data_length, |
| 700 |
&file_width,&file_height,&file_depth,&file_colors,&has_palette); |
| 701 |
}break; |
| 702 |
case OP_PIC_FORMAT_GIF:{ |
| 703 |
op_extract_gif_params(_buf+i,data_length, |
| 704 |
&file_width,&file_height,&file_depth,&file_colors,&has_palette); |
| 705 |
}break; |
| 706 |
} |
| 707 |
if(has_palette>=0){ |
| 708 |
/*If we successfully extracted these parameters from the image, override |
| 709 |
any declared values.*/ |
| 710 |
width=file_width; |
| 711 |
height=file_height; |
| 712 |
depth=file_depth; |
| 713 |
colors=file_colors; |
| 714 |
} |
| 715 |
/*Picture type 1 must be a 32x32 PNG.*/ |
| 716 |
if(picture_type==1&&(format!=OP_PIC_FORMAT_PNG||width!=32||height!=32)){ |
| 717 |
return OP_ENOTFORMAT; |
| 718 |
} |
| 719 |
} |
| 720 |
/*Adjust _buf_sz instead of using data_length to capture the terminating NUL |
| 721 |
for URLs.*/ |
| 722 |
_buf_sz-=i; |
| 723 |
memmove(_buf,_buf+i,sizeof(*_buf)*_buf_sz); |
| 724 |
_buf=(unsigned char *)Melder_realloc(_buf,_buf_sz); |
| 725 |
if(_buf_sz>0&&_buf==NULL)return OP_EFAULT; |
| 726 |
_pic->type=picture_type; |
| 727 |
_pic->width=width; |
| 728 |
_pic->height=height; |
| 729 |
_pic->depth=depth; |
| 730 |
_pic->colors=colors; |
| 731 |
_pic->data_length=data_length; |
| 732 |
_pic->data=_buf; |
| 733 |
_pic->format=format; |
| 734 |
return 0; |
| 735 |
} |
| 736 |
|
| 737 |
int opus_picture_tag_parse(OpusPictureTag *_pic,const char *_tag){ |
| 738 |
OpusPictureTag pic; |
| 739 |
unsigned char *buf; |
| 740 |
size_t base64_sz; |
| 741 |
size_t buf_sz; |
| 742 |
size_t tag_length; |
| 743 |
int ret; |
| 744 |
if(opus_tagncompare("METADATA_BLOCK_PICTURE",22,_tag)==0)_tag+=23; |
| 745 |
/*Figure out how much BASE64-encoded data we have.*/ |
| 746 |
tag_length=strlen(_tag); |
| 747 |
if(tag_length&3)return OP_ENOTFORMAT; |
| 748 |
base64_sz=tag_length>>2; |
| 749 |
buf_sz=3*base64_sz; |
| 750 |
if(buf_sz<32)return OP_ENOTFORMAT; |
| 751 |
if(_tag[tag_length-1]=='=')buf_sz--; |
| 752 |
if(_tag[tag_length-2]=='=')buf_sz--; |
| 753 |
if(buf_sz<32)return OP_ENOTFORMAT; |
| 754 |
/*Allocate an extra byte to allow appending a terminating NUL to URL data.*/ |
| 755 |
buf=(unsigned char *)_Melder_malloc(sizeof(*buf)*(buf_sz+1)); |
| 756 |
if(buf==NULL)return OP_EFAULT; |
| 757 |
opus_picture_tag_init(&pic); |
| 758 |
ret=opus_picture_tag_parse_impl(&pic,_tag,buf,buf_sz,base64_sz); |
| 759 |
if(ret<0){ |
| 760 |
opus_picture_tag_clear(&pic); |
| 761 |
Melder_free(buf); |
| 762 |
} |
| 763 |
else *_pic=*&pic; |
| 764 |
return ret; |
| 765 |
} |
| 766 |
|
| 767 |
void opus_picture_tag_init(OpusPictureTag *_pic){ |
| 768 |
memset(_pic,0,sizeof(*_pic)); |
| 769 |
} |
| 770 |
|
| 771 |
void opus_picture_tag_clear(OpusPictureTag *_pic){ |
| 772 |
Melder_free(_pic->description); |
| 773 |
Melder_free(_pic->mime_type); |
| 774 |
Melder_free(_pic->data); |
| 775 |
} |