annotate libass/ass_library.c @ 29385:f9ae25067fe0

Fix 24bit audio playback. The reordering channels code had reoccurring bug where in switch(samplesize) block the case 3 (3 bytes) doesn't end with break; leading to execution of the next case 4 too. This mangles the already processed data and causes massive memory corruption.
author iive
date Sun, 19 Jul 2009 09:55:29 +0000
parents 0f1b5b68af32
children 48d020c5ceca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*-
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
2 // vim:ts=8:sw=8:noet:ai:
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
3 /*
26723
0f892cd714b2 Use standard license header.
diego
parents: 25621
diff changeset
4 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
0f892cd714b2 Use standard license header.
diego
parents: 25621
diff changeset
5 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26724
diff changeset
6 * This file is part of libass.
26723
0f892cd714b2 Use standard license header.
diego
parents: 25621
diff changeset
7 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26724
diff changeset
8 * libass is free software; you can redistribute it and/or modify
26723
0f892cd714b2 Use standard license header.
diego
parents: 25621
diff changeset
9 * it under the terms of the GNU General Public License as published by
0f892cd714b2 Use standard license header.
diego
parents: 25621
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
0f892cd714b2 Use standard license header.
diego
parents: 25621
diff changeset
11 * (at your option) any later version.
0f892cd714b2 Use standard license header.
diego
parents: 25621
diff changeset
12 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26724
diff changeset
13 * libass is distributed in the hope that it will be useful,
26723
0f892cd714b2 Use standard license header.
diego
parents: 25621
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0f892cd714b2 Use standard license header.
diego
parents: 25621
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0f892cd714b2 Use standard license header.
diego
parents: 25621
diff changeset
16 * GNU General Public License for more details.
0f892cd714b2 Use standard license header.
diego
parents: 25621
diff changeset
17 *
0f892cd714b2 Use standard license header.
diego
parents: 25621
diff changeset
18 * You should have received a copy of the GNU General Public License along
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26724
diff changeset
19 * with libass; if not, write to the Free Software Foundation, Inc.,
26724
980ec8f69c58 Fix one more license header wording detail for consistency.
diego
parents: 26723
diff changeset
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26723
0f892cd714b2 Use standard license header.
diego
parents: 25621
diff changeset
21 */
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
22
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
23 #include <inttypes.h>
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
24 #include <stdio.h>
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
25 #include <stdlib.h>
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
26 #include <string.h>
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
27
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
28 #include "ass.h"
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
29 #include "ass_library.h"
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
30
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
31
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
32 ass_library_t* ass_library_init(void)
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
33 {
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
34 return calloc(1, sizeof(ass_library_t));
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
35 }
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
36
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
37 void ass_library_done(ass_library_t* priv)
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
38 {
21497
4a4af5271542 Also free ass_library_t members in ass_library_done
reimar
parents: 21458
diff changeset
39 if (priv) {
4a4af5271542 Also free ass_library_t members in ass_library_done
reimar
parents: 21458
diff changeset
40 ass_set_fonts_dir(priv, NULL);
4a4af5271542 Also free ass_library_t members in ass_library_done
reimar
parents: 21458
diff changeset
41 ass_set_style_overrides(priv, NULL);
25613
f00333e3facf Copy font data to ass_library instead of referencing demuxer-owned memory.
eugeni
parents: 21497
diff changeset
42 ass_clear_fonts(priv);
21497
4a4af5271542 Also free ass_library_t members in ass_library_done
reimar
parents: 21458
diff changeset
43 free(priv);
4a4af5271542 Also free ass_library_t members in ass_library_done
reimar
parents: 21458
diff changeset
44 }
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
45 }
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
46
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
47 void ass_set_fonts_dir(ass_library_t* priv, const char* fonts_dir)
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
48 {
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
49 if (priv->fonts_dir)
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
50 free(priv->fonts_dir);
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
51
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
52 priv->fonts_dir = fonts_dir ? strdup(fonts_dir) : 0;
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
53 }
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
54
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
55 void ass_set_extract_fonts(ass_library_t* priv, int extract)
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
56 {
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
57 priv->extract_fonts = !!extract;
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
58 }
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
59
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
60 void ass_set_style_overrides(ass_library_t* priv, char** list)
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
61 {
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
62 char** p;
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
63 char** q;
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
64 int cnt;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 26738
diff changeset
65
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
66 if (priv->style_overrides) {
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
67 for (p = priv->style_overrides; *p; ++p)
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
68 free(*p);
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
69 free(priv->style_overrides);
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
70 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 26738
diff changeset
71
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
72 if (!list) return;
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
73
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
74 for (p = list, cnt = 0; *p; ++p, ++cnt) {}
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
75
20722
2edba6772316 Bugfix: when copying ass_force_style_list, ending 0 was left out.
eugeni
parents: 20477
diff changeset
76 priv->style_overrides = malloc((cnt + 1) * sizeof(char*));
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
77 for (p = list, q = priv->style_overrides; *p; ++p, ++q)
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
78 *q = strdup(*p);
20722
2edba6772316 Bugfix: when copying ass_force_style_list, ending 0 was left out.
eugeni
parents: 20477
diff changeset
79 priv->style_overrides[cnt] = NULL;
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents:
diff changeset
80 }
21458
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 20722
diff changeset
81
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 20722
diff changeset
82 static void grow_array(void **array, int nelem, size_t elsize)
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 20722
diff changeset
83 {
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 20722
diff changeset
84 if (!(nelem & 31))
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 20722
diff changeset
85 *array = realloc(*array, (nelem + 32) * elsize);
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 20722
diff changeset
86 }
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 20722
diff changeset
87
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 20722
diff changeset
88 void ass_add_font(ass_library_t* priv, char* name, char* data, int size)
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 20722
diff changeset
89 {
25613
f00333e3facf Copy font data to ass_library instead of referencing demuxer-owned memory.
eugeni
parents: 21497
diff changeset
90 int idx = priv->num_fontdata;
f00333e3facf Copy font data to ass_library instead of referencing demuxer-owned memory.
eugeni
parents: 21497
diff changeset
91 if (!name || !data || !size)
f00333e3facf Copy font data to ass_library instead of referencing demuxer-owned memory.
eugeni
parents: 21497
diff changeset
92 return;
21458
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 20722
diff changeset
93 grow_array((void**)&priv->fontdata, priv->num_fontdata, sizeof(*priv->fontdata));
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 26738
diff changeset
94
25616
042b545d588e No need to reinvent strdup...
eugeni
parents: 25613
diff changeset
95 priv->fontdata[idx].name = strdup(name);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 26738
diff changeset
96
25613
f00333e3facf Copy font data to ass_library instead of referencing demuxer-owned memory.
eugeni
parents: 21497
diff changeset
97 priv->fontdata[idx].data = malloc(size);
f00333e3facf Copy font data to ass_library instead of referencing demuxer-owned memory.
eugeni
parents: 21497
diff changeset
98 memcpy(priv->fontdata[idx].data, data, size);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 26738
diff changeset
99
25613
f00333e3facf Copy font data to ass_library instead of referencing demuxer-owned memory.
eugeni
parents: 21497
diff changeset
100 priv->fontdata[idx].size = size;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 26738
diff changeset
101
21458
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 20722
diff changeset
102 priv->num_fontdata ++;
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 20722
diff changeset
103 }
7af6c25a0cfc Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents: 20722
diff changeset
104
25613
f00333e3facf Copy font data to ass_library instead of referencing demuxer-owned memory.
eugeni
parents: 21497
diff changeset
105 void ass_clear_fonts(ass_library_t* priv)
f00333e3facf Copy font data to ass_library instead of referencing demuxer-owned memory.
eugeni
parents: 21497
diff changeset
106 {
f00333e3facf Copy font data to ass_library instead of referencing demuxer-owned memory.
eugeni
parents: 21497
diff changeset
107 int i;
f00333e3facf Copy font data to ass_library instead of referencing demuxer-owned memory.
eugeni
parents: 21497
diff changeset
108 for (i = 0; i < priv->num_fontdata; ++i) {
f00333e3facf Copy font data to ass_library instead of referencing demuxer-owned memory.
eugeni
parents: 21497
diff changeset
109 free(priv->fontdata[i].name);
f00333e3facf Copy font data to ass_library instead of referencing demuxer-owned memory.
eugeni
parents: 21497
diff changeset
110 free(priv->fontdata[i].data);
f00333e3facf Copy font data to ass_library instead of referencing demuxer-owned memory.
eugeni
parents: 21497
diff changeset
111 }
25621
a09381e1a325 Fix ass_clear_fonts not deallocating fontdata.
eugeni
parents: 25616
diff changeset
112 free(priv->fontdata);
a09381e1a325 Fix ass_clear_fonts not deallocating fontdata.
eugeni
parents: 25616
diff changeset
113 priv->fontdata = NULL;
a09381e1a325 Fix ass_clear_fonts not deallocating fontdata.
eugeni
parents: 25616
diff changeset
114 priv->num_fontdata = 0;
25613
f00333e3facf Copy font data to ass_library instead of referencing demuxer-owned memory.
eugeni
parents: 21497
diff changeset
115 }