7 static uint32_t fix_byte_order_32(uint32_t src)
13 memcpy(data,&src,sizeof(src));
19 result = (a<<24) | (b<<16) | (c<<8) | d;
20 return (uint32_t)result;
23 static uint16_t fix_byte_order_16(uint16_t src)
29 memcpy(data,&src,sizeof(src));
34 return (uint16_t)result;
46 static unsigned membuffer_write(membuffer * buf,const void * ptr,unsigned bytes)
48 unsigned dest_size = buf->written + bytes;
50 if (buf->error) return 0;
51 if (dest_size > buf->allocated)
56 } while(dest_size > buf->allocated);
59 void * newptr = realloc(buf->data,buf->allocated);
71 if (ptr) memcpy((char*)buf->data + buf->written,ptr,bytes);
72 buf->written += bytes;
76 #define membuffer_write_data membuffer_write
78 static unsigned membuffer_write_int32(membuffer * buf,uint32_t data)
80 uint8_t temp[4] = {(uint8_t)(data>>24),(uint8_t)(data>>16),(uint8_t)(data>>8),(uint8_t)data};
81 return membuffer_write_data(buf,temp,4);
84 static unsigned membuffer_write_int24(membuffer * buf,uint32_t data)
86 uint8_t temp[3] = {(uint8_t)(data>>16),(uint8_t)(data>>8),(uint8_t)data};
87 return membuffer_write_data(buf,temp,3);
90 static unsigned membuffer_write_int16(membuffer * buf,uint16_t data)
92 uint8_t temp[2] = {(uint8_t)(data>>8),(uint8_t)data};
93 return membuffer_write_data(buf,temp,2);
96 static unsigned membuffer_write_atom_name(membuffer * buf,const char * data)
98 return membuffer_write_data(buf,data,4)==4 ? 1 : 0;
101 static void membuffer_write_atom(membuffer * buf,const char * name,unsigned size,const void * data)
103 membuffer_write_int32(buf,size + 8);
104 membuffer_write_atom_name(buf,name);
105 membuffer_write_data(buf,data,size);
108 static unsigned membuffer_write_string(membuffer * buf,const char * data)
110 return membuffer_write_data(buf,data,strlen(data));
113 static unsigned membuffer_write_int8(membuffer * buf,uint8_t data)
115 return membuffer_write_data(buf,&data,1);
118 static void * membuffer_get_ptr(const membuffer * buf)
123 static unsigned membuffer_get_size(const membuffer * buf)
128 static unsigned membuffer_error(const membuffer * buf)
133 static void membuffer_set_error(membuffer * buf) {buf->error = 1;}
135 static unsigned membuffer_transfer_from_file(membuffer * buf,mp4ff_t * src,unsigned bytes)
140 oldsize = membuffer_get_size(buf);
141 if (membuffer_write_data(buf,0,bytes) != bytes) return 0;
143 bufptr = membuffer_get_ptr(buf);
144 if (bufptr==0) return 0;
146 if ((unsigned)mp4ff_read_data(src,(char*)bufptr + oldsize,bytes)!=bytes)
148 membuffer_set_error(buf);
156 static membuffer * membuffer_create()
158 const unsigned initial_size = 256;
160 membuffer * buf = (membuffer *) malloc(sizeof(membuffer));
161 buf->data = malloc(initial_size);
163 buf->allocated = initial_size;
164 buf->error = buf->data == 0 ? 1 : 0;
169 static void membuffer_free(membuffer * buf)
171 if (buf->data) free(buf->data);
175 static void * membuffer_detach(membuffer * buf)
179 if (buf->error) return 0;
181 ret = realloc(buf->data,buf->written);
183 if (ret == 0) free(buf->data);
192 /* metadata tag structure */
199 /* metadata list structure */
213 static stdmeta_entry stdmetas[] =
223 {"cpil","compilation"},
231 static const char* find_standard_meta(const char * name) //returns atom name if found, 0 if not
234 for(n=0;n<sizeof(stdmetas)/sizeof(stdmetas[0]);n++)
236 if (!stricmp(name,stdmetas[n].name)) return stdmetas[n].atom;
241 static void membuffer_write_track_tag(membuffer * buf,const char * name,uint32_t index,uint32_t total)
243 membuffer_write_int32(buf,8 /*atom header*/ + 8 /*data atom header*/ + 8 /*flags + reserved*/ + 8 /*actual data*/ );
244 membuffer_write_atom_name(buf,name);
245 membuffer_write_int32(buf,8 /*data atom header*/ + 8 /*flags + reserved*/ + 8 /*actual data*/ );
246 membuffer_write_atom_name(buf,"data");
247 membuffer_write_int32(buf,0);//flags
248 membuffer_write_int32(buf,0);//reserved
249 membuffer_write_int16(buf,0);
250 membuffer_write_int16(buf,(uint16_t)index);//track number
251 membuffer_write_int16(buf,(uint16_t)total);//total tracks
252 membuffer_write_int16(buf,0);
255 static void membuffer_write_int16_tag(membuffer * buf,const char * name,uint16_t value)
257 membuffer_write_int32(buf,8 /*atom header*/ + 8 /*data atom header*/ + 8 /*flags + reserved*/ + 2 /*actual data*/ );
258 membuffer_write_atom_name(buf,name);
259 membuffer_write_int32(buf,8 /*data atom header*/ + 8 /*flags + reserved*/ + 2 /*actual data*/ );
260 membuffer_write_atom_name(buf,"data");
261 membuffer_write_int32(buf,0);//flags
262 membuffer_write_int32(buf,0);//reserved
263 membuffer_write_int16(buf,value);//value
266 static void membuffer_write_std_tag(membuffer * buf,const char * name,const char * value)
268 membuffer_write_int32(buf,8 /*atom header*/ + 8 /*data atom header*/ + 8 /*flags + reserved*/ + strlen(value) );
269 membuffer_write_atom_name(buf,name);
270 membuffer_write_int32(buf,8 /*data atom header*/ + 8 /*flags + reserved*/ + strlen(value));
271 membuffer_write_atom_name(buf,"data");
272 membuffer_write_int32(buf,1);//flags
273 membuffer_write_int32(buf,0);//reserved
274 membuffer_write_data(buf,value,strlen(value));
277 static void membuffer_write_custom_tag(membuffer * buf,const char * name,const char * value)
279 membuffer_write_int32(buf,8 /*atom header*/ + 0x1C /*weirdo itunes atom*/ + 12 /*name atom header*/ + strlen(name) + 16 /*data atom header + flags*/ + strlen(value) );
280 membuffer_write_atom_name(buf,"----");
281 membuffer_write_int32(buf,0x1C);//weirdo itunes atom
282 membuffer_write_atom_name(buf,"mean");
283 membuffer_write_int32(buf,0);
284 membuffer_write_data(buf,"com.apple.iTunes",16);
285 membuffer_write_int32(buf,12 + strlen(name));
286 membuffer_write_atom_name(buf,"name");
287 membuffer_write_int32(buf,0);
288 membuffer_write_data(buf,name,strlen(name));
289 membuffer_write_int32(buf,8 /*data atom header*/ + 8 /*flags + reserved*/ + strlen(value));
290 membuffer_write_atom_name(buf,"data");
291 membuffer_write_int32(buf,1);//flags
292 membuffer_write_int32(buf,0);//reserved
293 membuffer_write_data(buf,value,strlen(value));
297 static uint32_t myatoi(const char * param)
299 return param ? atoi(param) : 0;
302 static uint32_t create_ilst(const mp4ff_metadata_t * data,void ** out_buffer,uint32_t * out_size)
304 membuffer * buf = membuffer_create();
306 char * mask = (char*)malloc(data->count);
307 memset(mask,0,data->count);
310 const char * tracknumber_ptr = 0, * totaltracks_ptr = 0;
311 const char * discnumber_ptr = 0, * totaldiscs_ptr = 0;
312 const char * genre_ptr = 0, * tempo_ptr = 0;
313 for(metaptr = 0; metaptr < data->count; metaptr++)
315 mp4ff_tag_t * tag = &data->tags[metaptr];
316 if (!stricmp(tag->item,"tracknumber") || !stricmp(tag->item,"track"))
318 if (tracknumber_ptr==0) tracknumber_ptr = tag->value;
321 else if (!stricmp(tag->item,"totaltracks"))
323 if (totaltracks_ptr==0) totaltracks_ptr = tag->value;
326 else if (!stricmp(tag->item,"discnumber") || !stricmp(tag->item,"disc"))
328 if (discnumber_ptr==0) discnumber_ptr = tag->value;
331 else if (!stricmp(tag->item,"totaldiscs"))
333 if (totaldiscs_ptr==0) totaldiscs_ptr = tag->value;
336 else if (!stricmp(tag->item,"genre"))
338 if (genre_ptr==0) genre_ptr = tag->value;
341 else if (!stricmp(tag->item,"tempo"))
343 if (tempo_ptr==0) tempo_ptr = tag->value;
349 if (tracknumber_ptr) membuffer_write_track_tag(buf,"trkn",myatoi(tracknumber_ptr),myatoi(totaltracks_ptr));
350 if (discnumber_ptr) membuffer_write_track_tag(buf,"disk",myatoi(discnumber_ptr),myatoi(totaldiscs_ptr));
351 if (tempo_ptr) membuffer_write_int16_tag(buf,"tmpo",(uint16_t)myatoi(tempo_ptr));
355 uint32_t index = mp4ff_meta_genre_to_index(genre_ptr);
357 membuffer_write_std_tag(buf,"©gen",genre_ptr);
359 membuffer_write_int16_tag(buf,"gnre",(uint16_t)index);
363 for(metaptr = 0; metaptr < data->count; metaptr++)
367 mp4ff_tag_t * tag = &data->tags[metaptr];
368 const char * std_meta_atom = find_standard_meta(tag->item);
371 membuffer_write_std_tag(buf,std_meta_atom,tag->value);
375 membuffer_write_custom_tag(buf,tag->item,tag->value);
382 if (membuffer_error(buf))
388 *out_size = membuffer_get_size(buf);
389 *out_buffer = membuffer_detach(buf);
395 static uint32_t find_atom(mp4ff_t * f,uint64_t base,uint32_t size,const char * name)
397 uint32_t remaining = size;
398 uint64_t atom_offset = base;
404 mp4ff_set_position(f,atom_offset);
406 if (remaining < 8) break;
407 atom_size = mp4ff_read_int32(f);
408 if (atom_size > remaining || atom_size < 8) break;
409 mp4ff_read_data(f,atom_name,4);
411 if (!memcmp(atom_name,name,4))
413 mp4ff_set_position(f,atom_offset);
417 remaining -= atom_size;
418 atom_offset += atom_size;
423 static uint32_t find_atom_v2(mp4ff_t * f,uint64_t base,uint32_t size,const char * name,uint32_t extraheaders,const char * name_inside)
425 uint64_t first_base = (uint64_t)(-1);
426 while(find_atom(f,base,size,name))//try to find atom <name> with atom <name_inside> in it
428 uint64_t mybase = mp4ff_position(f);
429 uint32_t mysize = mp4ff_read_int32(f);
431 if (first_base == (uint64_t)(-1)) first_base = mybase;
433 if (mysize < 8 + extraheaders) break;
435 if (find_atom(f,mybase+(8+extraheaders),mysize-(8+extraheaders),name_inside))
437 mp4ff_set_position(f,mybase);
441 if (size<=mysize) {size=0;break;}
445 if (first_base != (uint64_t)(-1))//wanted atom inside not found
447 mp4ff_set_position(f,first_base);
453 static uint32_t create_meta(const mp4ff_metadata_t * data,void ** out_buffer,uint32_t * out_size)
459 if (!create_ilst(data,&ilst_buffer,&ilst_size)) return 0;
461 buf = membuffer_create();
463 membuffer_write_int32(buf,0);
464 membuffer_write_atom(buf,"ilst",ilst_size,ilst_buffer);
467 *out_size = membuffer_get_size(buf);
468 *out_buffer = membuffer_detach(buf);
473 static uint32_t create_udta(const mp4ff_metadata_t * data,void ** out_buffer,uint32_t * out_size)
479 if (!create_meta(data,&meta_buffer,&meta_size)) return 0;
481 buf = membuffer_create();
483 membuffer_write_atom(buf,"meta",meta_size,meta_buffer);
487 *out_size = membuffer_get_size(buf);
488 *out_buffer = membuffer_detach(buf);
493 static uint32_t modify_moov(mp4ff_t * f,const mp4ff_metadata_t * data,void ** out_buffer,uint32_t * out_size)
495 uint64_t total_base = f->moov_offset + 8;
496 uint32_t total_size = (uint32_t)(f->moov_size - 8);
498 uint64_t udta_offset,meta_offset,ilst_offset;
499 uint32_t udta_size, meta_size, ilst_size;
501 uint32_t new_ilst_size;
502 void * new_ilst_buffer;
508 if (!find_atom_v2(f,total_base,total_size,"udta",0,"meta"))
511 void * new_udta_buffer;
512 uint32_t new_udta_size;
513 if (!create_udta(data,&new_udta_buffer,&new_udta_size)) return 0;
515 buf = membuffer_create();
516 mp4ff_set_position(f,total_base);
517 membuffer_transfer_from_file(buf,f,total_size);
519 membuffer_write_atom(buf,"udta",new_udta_size,new_udta_buffer);
521 free(new_udta_buffer);
523 *out_size = membuffer_get_size(buf);
524 *out_buffer = membuffer_detach(buf);
530 udta_offset = mp4ff_position(f);
531 udta_size = mp4ff_read_int32(f);
532 if (find_atom_v2(f,udta_offset+8,udta_size-8,"meta",4,"ilst")<2)
535 void * new_meta_buffer;
536 uint32_t new_meta_size;
537 if (!create_meta(data,&new_meta_buffer,&new_meta_size)) return 0;
539 buf = membuffer_create();
540 mp4ff_set_position(f,total_base);
541 membuffer_transfer_from_file(buf,f,(uint32_t)(udta_offset - total_base));
543 membuffer_write_int32(buf,udta_size + 8 + new_meta_size);
544 membuffer_write_atom_name(buf,"udta");
545 membuffer_transfer_from_file(buf,f,udta_size);
547 membuffer_write_atom(buf,"meta",new_meta_size,new_meta_buffer);
548 free(new_meta_buffer);
550 *out_size = membuffer_get_size(buf);
551 *out_buffer = membuffer_detach(buf);
555 meta_offset = mp4ff_position(f);
556 meta_size = mp4ff_read_int32(f);
557 if (!find_atom(f,meta_offset+12,meta_size-12,"ilst")) return 0;//shouldn't happen, find_atom_v2 above takes care of it
558 ilst_offset = mp4ff_position(f);
559 ilst_size = mp4ff_read_int32(f);
561 if (!create_ilst(data,&new_ilst_buffer,&new_ilst_size)) return 0;
563 size_delta = new_ilst_size - (ilst_size - 8);
565 *out_size = total_size + size_delta;
566 *out_buffer = malloc(*out_size);
567 if (*out_buffer == 0)
569 free(new_ilst_buffer);
573 p_out = (uint8_t*)*out_buffer;
575 mp4ff_set_position(f,total_base);
576 mp4ff_read_data(f,p_out,(uint32_t)(udta_offset - total_base )); p_out += (uint32_t)(udta_offset - total_base );
577 *(uint32_t*)p_out = fix_byte_order_32(mp4ff_read_int32(f) + size_delta); p_out += 4;
578 mp4ff_read_data(f,p_out,4); p_out += 4;
579 mp4ff_read_data(f,p_out,(uint32_t)(meta_offset - udta_offset - 8)); p_out += (uint32_t)(meta_offset - udta_offset - 8);
580 *(uint32_t*)p_out = fix_byte_order_32(mp4ff_read_int32(f) + size_delta); p_out += 4;
581 mp4ff_read_data(f,p_out,4); p_out += 4;
582 mp4ff_read_data(f,p_out,(uint32_t)(ilst_offset - meta_offset - 8)); p_out += (uint32_t)(ilst_offset - meta_offset - 8);
583 *(uint32_t*)p_out = fix_byte_order_32(mp4ff_read_int32(f) + size_delta); p_out += 4;
584 mp4ff_read_data(f,p_out,4); p_out += 4;
586 memcpy(p_out,new_ilst_buffer,new_ilst_size);
587 p_out += new_ilst_size;
589 mp4ff_set_position(f,ilst_offset + ilst_size);
590 mp4ff_read_data(f,p_out,(uint32_t)(total_size - (ilst_offset - total_base) - ilst_size));
592 free(new_ilst_buffer);
598 static int32_t mp4ff_meta_update(mp4ff_callback_t *f,const mp4ff_metadata_t * data)
600 void * new_moov_data;
601 uint32_t new_moov_size;
603 mp4ff_t *ff = malloc(sizeof(mp4ff_t));
605 memset(ff, 0, sizeof(mp4ff_t));
607 mp4ff_set_position(ff,0);
612 if (!modify_moov(ff,data,&new_moov_data,&new_moov_size))
618 /* copy moov atom to end of the file */
619 if (ff->last_atom != ATOM_MOOV)
621 char *free_data = "free";
623 /* rename old moov to free */
624 mp4ff_set_position(ff, ff->moov_offset + 4);
625 mp4ff_write_data(ff, free_data, 4);
627 mp4ff_set_position(ff, ff->file_size);
628 mp4ff_write_int32(ff,new_moov_size + 8);
629 mp4ff_write_data(ff,"moov",4);
630 mp4ff_write_data(ff, new_moov_data, new_moov_size);
634 mp4ff_set_position(ff, ff->moov_offset);
635 mp4ff_write_int32(ff,new_moov_size + 8);
636 mp4ff_write_data(ff,"moov",4);
637 mp4ff_write_data(ff, new_moov_data, new_moov_size);