aboutsummaryrefslogblamecommitdiffstats
path: root/src/mp4ff/mp4tagupdate.c
blob: be2d7084b0644a897eefff2f6882ed023fe73c8b (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645




































































































































































































































































































































































































































































































































































































































































                                                                                                                                                                               
#include <stdlib.h>
#include <string.h>
#include "mp4ffint.h"

#ifdef USE_TAGGING

static uint32_t fix_byte_order_32(uint32_t src)
{
    uint32_t result;
    uint32_t a, b, c, d;
    int8_t data[4];
    
    memcpy(data,&src,sizeof(src));
    a = (uint8_t)data[0];
    b = (uint8_t)data[1];
    c = (uint8_t)data[2];
    d = (uint8_t)data[3];

    result = (a<<24) | (b<<16) | (c<<8) | d;
    return (uint32_t)result;
}

static uint16_t fix_byte_order_16(uint16_t src)
{
    uint16_t result;
    uint16_t a, b;
    int8_t data[2];
    
    memcpy(data,&src,sizeof(src));
    a = (uint8_t)data[0];
    b = (uint8_t)data[1];

    result = (a<<8) | b;
    return (uint16_t)result;
}


typedef struct
{
	void * data;
	unsigned written;
	unsigned allocated;
	unsigned error;
} membuffer;

unsigned membuffer_write(membuffer * buf,const void * ptr,unsigned bytes)
{
	unsigned dest_size = buf->written + bytes;

	if (buf->error) return 0;
	if (dest_size > buf->allocated)
	{
		do
		{
			buf->allocated <<= 1;
		} while(dest_size > buf->allocated);
		
		{
			void * newptr = realloc(buf->data,buf->allocated);
			if (newptr==0)
			{
				free(buf->data);
				buf->data = 0;
				buf->error = 1;
				return 0;
			}
			buf->data = newptr;
		}
	}

	if (ptr) memcpy((char*)buf->data + buf->written,ptr,bytes);
	buf->written += bytes;
	return bytes;
}

#define membuffer_write_data membuffer_write

unsigned membuffer_write_int32(membuffer * buf,uint32_t data)
{
	uint8_t temp[4] = {(uint8_t)(data>>24),(uint8_t)(data>>16),(uint8_t)(data>>8),(uint8_t)data};	
	return membuffer_write_data(buf,temp,4);
}

unsigned membuffer_write_int24(membuffer * buf,uint32_t data)
{
	uint8_t temp[3] = {(uint8_t)(data>>16),(uint8_t)(data>>8),(uint8_t)data};
	return membuffer_write_data(buf,temp,3);
}

unsigned membuffer_write_int16(membuffer * buf,uint16_t data)
{
	uint8_t temp[2] = {(uint8_t)(data>>8),(uint8_t)data};
	return membuffer_write_data(buf,temp,2);
}

unsigned membuffer_write_atom_name(membuffer * buf,const char * data)
{
	return membuffer_write_data(buf,data,4)==4 ? 1 : 0;
}

void membuffer_write_atom(membuffer * buf,const char * name,unsigned size,const void * data)
{
	membuffer_write_int32(buf,size + 8);
	membuffer_write_atom_name(buf,name);
	membuffer_write_data(buf,data,size);
}

unsigned membuffer_write_string(membuffer * buf,const char * data)
{
	return membuffer_write_data(buf,data,strlen(data));
}

unsigned membuffer_write_int8(membuffer * buf,uint8_t data)
{
	return membuffer_write_data(buf,&data,1);
}

void * membuffer_get_ptr(const membuffer * buf)
{
	return buf->data;
}

unsigned membuffer_get_size(const membuffer * buf)
{
	return buf->written;
}

unsigned membuffer_error(const membuffer * buf)
{
	return buf->error;
}

void membuffer_set_error(membuffer * buf) {buf->error = 1;}

unsigned membuffer_transfer_from_file(membuffer * buf,mp4ff_t * src,unsigned bytes)
{
	unsigned oldsize;
	void * bufptr;
	
	oldsize = membuffer_get_size(buf);
	if (membuffer_write_data(buf,0,bytes) != bytes) return 0;

	bufptr = membuffer_get_ptr(buf);
	if (bufptr==0) return 0;
	
	if ((unsigned)mp4ff_read_data(src,(char*)bufptr + oldsize,bytes)!=bytes)
	{
		membuffer_set_error(buf);
		return 0;
	}
	
	return bytes;
}


membuffer * membuffer_create()
{
	const unsigned initial_size = 256;

	membuffer * buf = (membuffer *) malloc(sizeof(membuffer));
	buf->data = malloc(initial_size);
	buf->written = 0;
	buf->allocated = initial_size;
	buf->error = buf->data == 0 ? 1 : 0;

	return buf;
}

void membuffer_free(membuffer * buf)
{
	if (buf->data) free(buf->data);
	free(buf);
}

void * membuffer_detach(membuffer * buf)
{
	void * ret;

	if (buf->error) return 0;

	ret = realloc(buf->data,buf->written);
	
	if (ret == 0) free(buf->data);

	buf->data = 0;
	buf->error = 1;
	
	return ret;
}

#if 0
/* metadata tag structure */
typedef struct
{
    char *item;
    char *value;
} mp4ff_tag_t;

/* metadata list structure */
typedef struct
{
    mp4ff_tag_t *tags;
    uint32_t count;
} mp4ff_metadata_t;
#endif

typedef struct
{
	const char * atom;
	const char * name;	
} stdmeta_entry;

static stdmeta_entry stdmetas[] = 
{
	{"�nam","title"},
	{"�ART","artist"},
	{"�wrt","writer"},
	{"�alb","album"},
	{"�day","date"},
	{"�too","tool"},
	{"�cmt","comment"},
//	{"�gen","genre"},
	{"cpil","compilation"},
//	{"trkn","track"},
//	{"disk","disc"},
//	{"gnre","genre"},
	{"covr","cover"},
};


static const char* find_standard_meta(const char * name) //returns atom name if found, 0 if not
{
	unsigned n;
	for(n=0;n<sizeof(stdmetas)/sizeof(stdmetas[0]);n++)
	{
		if (!stricmp(name,stdmetas[n].name)) return stdmetas[n].atom;
	}
    return 0;
}

static void membuffer_write_track_tag(membuffer * buf,const char * name,uint32_t index,uint32_t total)
{
	membuffer_write_int32(buf,8 /*atom header*/ + 8 /*data atom header*/ + 8 /*flags + reserved*/ + 8 /*actual data*/ );
	membuffer_write_atom_name(buf,name);
	membuffer_write_int32(buf,8 /*data atom header*/ + 8 /*flags + reserved*/ + 8 /*actual data*/ );
	membuffer_write_atom_name(buf,"data");
	membuffer_write_int32(buf,0);//flags
	membuffer_write_int32(buf,0);//reserved
	membuffer_write_int16(buf,0);
	membuffer_write_int16(buf,(uint16_t)index);//track number
	membuffer_write_int16(buf,(uint16_t)total);//total tracks
	membuffer_write_int16(buf,0);
}

static void membuffer_write_int16_tag(membuffer * buf,const char * name,uint16_t value)
{
	membuffer_write_int32(buf,8 /*atom header*/ + 8 /*data atom header*/ + 8 /*flags + reserved*/ + 2 /*actual data*/ );
	membuffer_write_atom_name(buf,name);
	membuffer_write_int32(buf,8 /*data atom header*/ + 8 /*flags + reserved*/ + 2 /*actual data*/ );
	membuffer_write_atom_name(buf,"data");
	membuffer_write_int32(buf,0);//flags
	membuffer_write_int32(buf,0);//reserved
	membuffer_write_int16(buf,value);//value
}

static void membuffer_write_std_tag(membuffer * buf,const char * name,const char * value)
{
	membuffer_write_int32(buf,8 /*atom header*/ + 8 /*data atom header*/ + 8 /*flags + reserved*/ + strlen(value) );
	membuffer_write_atom_name(buf,name);
	membuffer_write_int32(buf,8 /*data atom header*/ + 8 /*flags + reserved*/ + strlen(value));
	membuffer_write_atom_name(buf,"data");
	membuffer_write_int32(buf,1);//flags
	membuffer_write_int32(buf,0);//reserved
	membuffer_write_data(buf,value,strlen(value));
}

static void membuffer_write_custom_tag(membuffer * buf,const char * name,const char * value)
{
	membuffer_write_int32(buf,8 /*atom header*/ + 0x1C /*weirdo itunes atom*/ + 12 /*name atom header*/ + strlen(name) + 16 /*data atom header + flags*/ + strlen(value) );
	membuffer_write_atom_name(buf,"----");
	membuffer_write_int32(buf,0x1C);//weirdo itunes atom
	membuffer_write_atom_name(buf,"mean");
	membuffer_write_int32(buf,0);
	membuffer_write_data(buf,"com.apple.iTunes",16);
	membuffer_write_int32(buf,12 + strlen(name));
	membuffer_write_atom_name(buf,"name");
	membuffer_write_int32(buf,0);
	membuffer_write_data(buf,name,strlen(name));
	membuffer_write_int32(buf,8 /*data atom header*/ + 8 /*flags + reserved*/ + strlen(value));
	membuffer_write_atom_name(buf,"data");
	membuffer_write_int32(buf,1);//flags
	membuffer_write_int32(buf,0);//reserved
	membuffer_write_data(buf,value,strlen(value));

}

static uint32_t myatoi(const char * param)
{
	return param ? atoi(param) : 0;
}

static uint32_t create_ilst(const mp4ff_metadata_t * data,void ** out_buffer,uint32_t * out_size)
{
	membuffer * buf = membuffer_create();
	unsigned metaptr;
	char * mask = (char*)malloc(data->count);
	memset(mask,0,data->count);

	{
		const char * tracknumber_ptr = 0, * totaltracks_ptr = 0;
		const char * discnumber_ptr = 0, * totaldiscs_ptr = 0;
		const char * genre_ptr = 0, * tempo_ptr = 0;
		for(metaptr = 0; metaptr < data->count; metaptr++)
		{
			mp4ff_tag_t * tag = &data->tags[metaptr];
			if (!stricmp(tag->item,"tracknumber") || !stricmp(tag->item,"track"))
			{
				if (tracknumber_ptr==0) tracknumber_ptr = tag->value;
				mask[metaptr] = 1;
			}
			else if (!stricmp(tag->item,"totaltracks"))
			{
				if (totaltracks_ptr==0) totaltracks_ptr = tag->value;
				mask[metaptr] = 1;
			}
			else if (!stricmp(tag->item,"discnumber") || !stricmp(tag->item,"disc"))
			{
				if (discnumber_ptr==0) discnumber_ptr = tag->value;
				mask[metaptr] = 1;
			}
			else if (!stricmp(tag->item,"totaldiscs"))
			{
				if (totaldiscs_ptr==0) totaldiscs_ptr = tag->value;
				mask[metaptr] = 1;
			}
			else if (!stricmp(tag->item,"genre"))
			{
				if (genre_ptr==0) genre_ptr = tag->value;
				mask[metaptr] = 1;
			}
			else if (!stricmp(tag->item,"tempo"))
			{
				if (tempo_ptr==0) tempo_ptr = tag->value;
				mask[metaptr] = 1;
			}

		}

		if (tracknumber_ptr) membuffer_write_track_tag(buf,"trkn",myatoi(tracknumber_ptr),myatoi(totaltracks_ptr));
		if (discnumber_ptr) membuffer_write_track_tag(buf,"disk",myatoi(discnumber_ptr),myatoi(totaldiscs_ptr));
		if (tempo_ptr) membuffer_write_int16_tag(buf,"tmpo",(uint16_t)myatoi(tempo_ptr));

		if (genre_ptr)
		{
			uint32_t index = mp4ff_meta_genre_to_index(genre_ptr);
			if (index==0)
				membuffer_write_std_tag(buf,"�gen",genre_ptr);
			else
				membuffer_write_int16_tag(buf,"gnre",(uint16_t)index);
		}
	}
	
	for(metaptr = 0; metaptr < data->count; metaptr++)
	{
		if (!mask[metaptr])
		{
			mp4ff_tag_t * tag = &data->tags[metaptr];
			const char * std_meta_atom = find_standard_meta(tag->item);
			if (std_meta_atom)
			{
				membuffer_write_std_tag(buf,std_meta_atom,tag->value);
			}
			else
			{
				membuffer_write_custom_tag(buf,tag->item,tag->value);
			}
		}
	}

	free(mask);

	if (membuffer_error(buf))
	{
		membuffer_free(buf);
		return 0;
	}

	*out_size = membuffer_get_size(buf);
	*out_buffer = membuffer_detach(buf);
	membuffer_free(buf);

	return 1;
}

static uint32_t find_atom(mp4ff_t * f,uint64_t base,uint32_t size,const char * name)
{
	uint32_t remaining = size;
	uint64_t atom_offset = base;
	for(;;)
	{
		char atom_name[4];
		uint32_t atom_size;

		mp4ff_set_position(f,atom_offset);
		
		if (remaining < 8) break;
		atom_size = mp4ff_read_int32(f);
		if (atom_size > remaining || atom_size < 8) break;
		mp4ff_read_data(f,atom_name,4);
		
		if (!memcmp(atom_name,name,4))
		{
			mp4ff_set_position(f,atom_offset);
			return 1;
		}
		
		remaining -= atom_size;
		atom_offset += atom_size;
	}
	return 0;
}

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)
{
	uint64_t first_base = (uint64_t)(-1);
	while(find_atom(f,base,size,name))//try to find atom <name> with atom <name_inside> in it
	{
		uint64_t mybase = mp4ff_position(f);
		uint32_t mysize = mp4ff_read_int32(f);

		if (first_base == (uint64_t)(-1)) first_base = mybase;

		if (mysize < 8 + extraheaders) break;

		if (find_atom(f,mybase+(8+extraheaders),mysize-(8+extraheaders),name_inside))
		{
			mp4ff_set_position(f,mybase);
			return 2;
		}
		base += mysize;
		if (size<=mysize) {size=0;break;}
		size -= mysize;
	}

	if (first_base != (uint64_t)(-1))//wanted atom inside not found
	{
		mp4ff_set_position(f,first_base);
		return 1;
	}
	else return 0;	
}

static uint32_t create_meta(const mp4ff_metadata_t * data,void ** out_buffer,uint32_t * out_size)
{
	membuffer * buf;
	uint32_t ilst_size;
	void * ilst_buffer;

	if (!create_ilst(data,&ilst_buffer,&ilst_size)) return 0;

	buf = membuffer_create();

	membuffer_write_int32(buf,0);
	membuffer_write_atom(buf,"ilst",ilst_size,ilst_buffer);
	free(ilst_buffer);

	*out_size = membuffer_get_size(buf);
	*out_buffer = membuffer_detach(buf);
	membuffer_free(buf);
	return 1;
}

static uint32_t create_udta(const mp4ff_metadata_t * data,void ** out_buffer,uint32_t * out_size)
{
	membuffer * buf;
	uint32_t meta_size;
	void * meta_buffer;

	if (!create_meta(data,&meta_buffer,&meta_size)) return 0;

	buf = membuffer_create();

	membuffer_write_atom(buf,"meta",meta_size,meta_buffer);

	free(meta_buffer);

	*out_size = membuffer_get_size(buf);
	*out_buffer = membuffer_detach(buf);
	membuffer_free(buf);
	return 1;
}

static uint32_t modify_moov(mp4ff_t * f,const mp4ff_metadata_t * data,void ** out_buffer,uint32_t * out_size)
{
	uint64_t total_base = f->moov_offset + 8;
	uint32_t total_size = (uint32_t)(f->moov_size - 8);

	uint64_t udta_offset,meta_offset,ilst_offset;
	uint32_t udta_size,  meta_size,  ilst_size;
	
	uint32_t new_ilst_size;
	void * new_ilst_buffer;
	
	uint8_t * p_out;
	int32_t size_delta;
	
	
	if (!find_atom_v2(f,total_base,total_size,"udta",0,"meta"))
	{
		membuffer * buf;
		void * new_udta_buffer;
		uint32_t new_udta_size;
		if (!create_udta(data,&new_udta_buffer,&new_udta_size)) return 0;
		
		buf = membuffer_create();
		mp4ff_set_position(f,total_base);
		membuffer_transfer_from_file(buf,f,total_size);
		
		membuffer_write_atom(buf,"udta",new_udta_size,new_udta_buffer);

		free(new_udta_buffer);
	
		*out_size = membuffer_get_size(buf);
		*out_buffer = membuffer_detach(buf);
		membuffer_free(buf);
		return 1;		
	}
	else
	{
		udta_offset = mp4ff_position(f);
		udta_size = mp4ff_read_int32(f);
		if (find_atom_v2(f,udta_offset+8,udta_size-8,"meta",4,"ilst")<2)
		{
			membuffer * buf;
			void * new_meta_buffer;
			uint32_t new_meta_size;
			if (!create_meta(data,&new_meta_buffer,&new_meta_size)) return 0;
			
			buf = membuffer_create();
			mp4ff_set_position(f,total_base);
			membuffer_transfer_from_file(buf,f,(uint32_t)(udta_offset - total_base));
			
			membuffer_write_int32(buf,udta_size + 8 + new_meta_size);
			membuffer_write_atom_name(buf,"udta");
			membuffer_transfer_from_file(buf,f,udta_size);
						
			membuffer_write_atom(buf,"meta",new_meta_size,new_meta_buffer);
			free(new_meta_buffer);
		
			*out_size = membuffer_get_size(buf);
			*out_buffer = membuffer_detach(buf);
			membuffer_free(buf);
			return 1;		
		}
		meta_offset = mp4ff_position(f);
		meta_size = mp4ff_read_int32(f);
		if (!find_atom(f,meta_offset+12,meta_size-12,"ilst")) return 0;//shouldn't happen, find_atom_v2 above takes care of it
		ilst_offset = mp4ff_position(f);
		ilst_size = mp4ff_read_int32(f);

		if (!create_ilst(data,&new_ilst_buffer,&new_ilst_size)) return 0;
		
		size_delta = new_ilst_size - (ilst_size - 8);

		*out_size = total_size + size_delta;
		*out_buffer = malloc(*out_size);
		if (*out_buffer == 0)
		{
			free(new_ilst_buffer);
			return 0;
		}

		p_out = (uint8_t*)*out_buffer;
		
		mp4ff_set_position(f,total_base);
		mp4ff_read_data(f,p_out,(uint32_t)(udta_offset - total_base )); p_out += (uint32_t)(udta_offset - total_base );
		*(uint32_t*)p_out = fix_byte_order_32(mp4ff_read_int32(f) + size_delta); p_out += 4;
		mp4ff_read_data(f,p_out,4); p_out += 4;
		mp4ff_read_data(f,p_out,(uint32_t)(meta_offset - udta_offset - 8)); p_out += (uint32_t)(meta_offset - udta_offset - 8);
		*(uint32_t*)p_out = fix_byte_order_32(mp4ff_read_int32(f) + size_delta); p_out += 4;
		mp4ff_read_data(f,p_out,4); p_out += 4;
		mp4ff_read_data(f,p_out,(uint32_t)(ilst_offset - meta_offset - 8)); p_out += (uint32_t)(ilst_offset - meta_offset - 8);
		*(uint32_t*)p_out = fix_byte_order_32(mp4ff_read_int32(f) + size_delta); p_out += 4;
		mp4ff_read_data(f,p_out,4); p_out += 4;

		memcpy(p_out,new_ilst_buffer,new_ilst_size);
		p_out += new_ilst_size;

		mp4ff_set_position(f,ilst_offset + ilst_size);
		mp4ff_read_data(f,p_out,(uint32_t)(total_size - (ilst_offset - total_base) - ilst_size));

		free(new_ilst_buffer);
	}
	return 1;

}

int32_t mp4ff_meta_update(mp4ff_callback_t *f,const mp4ff_metadata_t * data)
{
	void * new_moov_data;
	uint32_t new_moov_size;

    mp4ff_t *ff = malloc(sizeof(mp4ff_t));

    memset(ff, 0, sizeof(mp4ff_t));
    ff->stream = f;
	mp4ff_set_position(ff,0);

    parse_atoms(ff);


	if (!modify_moov(ff,data,&new_moov_data,&new_moov_size))
	{
		mp4ff_close(ff);
		return 0;
	}

    /* copy moov atom to end of the file */
    if (ff->last_atom != ATOM_MOOV)
    {
        char *free_data = "free";

        /* rename old moov to free */
        mp4ff_set_position(ff, ff->moov_offset + 4);
        mp4ff_write_data(ff, free_data, 4);
	
        mp4ff_set_position(ff, ff->file_size);
		mp4ff_write_int32(ff,new_moov_size + 8);
		mp4ff_write_data(ff,"moov",4);
		mp4ff_write_data(ff, new_moov_data, new_moov_size);
    }
	else
	{
        mp4ff_set_position(ff, ff->moov_offset);
		mp4ff_write_int32(ff,new_moov_size + 8);
		mp4ff_write_data(ff,"moov",4);
		mp4ff_write_data(ff, new_moov_data, new_moov_size);
	}

	mp4ff_truncate(ff);

	mp4ff_close(ff);
    return 1;
}
#endif