Statistics
| Branch: | Revision:

ffmpeg / libavfilter / formats.c @ e73127a4

History | View | Annotate | Download (4.47 KB)

1 39135465 Vitor Sessak
/*
2
 * Filter layer - format negotiation
3 3fa77bde Stefano Sabatini
 * Copyright (c) 2007 Bobby Bingham
4 39135465 Vitor Sessak
 *
5 2912e87a Mans Rullgard
 * This file is part of Libav.
6 39135465 Vitor Sessak
 *
7 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
8 39135465 Vitor Sessak
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
13 39135465 Vitor Sessak
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
19 39135465 Vitor Sessak
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22 5ff84183 Stefano Sabatini
#include "libavutil/pixdesc.h"
23 39135465 Vitor Sessak
#include "avfilter.h"
24
25 4c4de9ca Michael Niedermayer
/**
26
 * Add all refs from a to ret and destroy a.
27
 */
28 37e0b997 Vitor Sessak
static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
29
{
30
    int i;
31 93faa9fa Vitor Sessak
32 37e0b997 Vitor Sessak
    for(i = 0; i < a->refcount; i ++) {
33
        ret->refs[ret->refcount] = a->refs[i];
34
        *ret->refs[ret->refcount++] = ret;
35
    }
36 93faa9fa Vitor Sessak
37
    av_free(a->refs);
38
    av_free(a->formats);
39
    av_free(a);
40 37e0b997 Vitor Sessak
}
41
42 39135465 Vitor Sessak
AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
43
{
44
    AVFilterFormats *ret;
45
    unsigned i, j, k = 0;
46
47
    ret = av_mallocz(sizeof(AVFilterFormats));
48
49
    /* merge list of formats */
50 243370cb Vitor Sessak
    ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
51
                                                           b->format_count));
52 39135465 Vitor Sessak
    for(i = 0; i < a->format_count; i ++)
53
        for(j = 0; j < b->format_count; j ++)
54
            if(a->formats[i] == b->formats[j])
55
                ret->formats[k++] = a->formats[i];
56
57 9189411b Vitor Sessak
    ret->format_count = k;
58 39135465 Vitor Sessak
    /* check that there was at least one common format */
59 9189411b Vitor Sessak
    if(!ret->format_count) {
60 39135465 Vitor Sessak
        av_free(ret->formats);
61
        av_free(ret);
62
        return NULL;
63
    }
64
65
    ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
66 37e0b997 Vitor Sessak
67
    merge_ref(ret, a);
68
    merge_ref(ret, b);
69 39135465 Vitor Sessak
70
    return ret;
71
}
72
73 bdab614b S.N. Hemanth Meenakshisundaram
AVFilterFormats *avfilter_make_format_list(const int *fmts)
74 f6a1fa85 Stefano Sabatini
{
75
    AVFilterFormats *formats;
76
    int count;
77
78 bdab614b S.N. Hemanth Meenakshisundaram
    for (count = 0; fmts[count] != -1; count++)
79 f6a1fa85 Stefano Sabatini
        ;
80
81
    formats               = av_mallocz(sizeof(AVFilterFormats));
82
    formats->formats      = av_malloc(sizeof(*formats->formats) * count);
83
    formats->format_count = count;
84 bdab614b S.N. Hemanth Meenakshisundaram
    memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
85 f6a1fa85 Stefano Sabatini
86
    return formats;
87
}
88
89 bdab614b S.N. Hemanth Meenakshisundaram
int avfilter_add_format(AVFilterFormats **avff, int fmt)
90 4fd1f187 Stefano Sabatini
{
91 bdab614b S.N. Hemanth Meenakshisundaram
    int *fmts;
92 4fd1f187 Stefano Sabatini
93 c1d662fd Stefano Sabatini
    if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
94
        return AVERROR(ENOMEM);
95
96 bdab614b S.N. Hemanth Meenakshisundaram
    fmts = av_realloc((*avff)->formats,
97
                      sizeof((*avff)->formats) * ((*avff)->format_count+1));
98
    if (!fmts)
99 4fd1f187 Stefano Sabatini
        return AVERROR(ENOMEM);
100
101 bdab614b S.N. Hemanth Meenakshisundaram
    (*avff)->formats = fmts;
102
    (*avff)->formats[(*avff)->format_count++] = fmt;
103 4fd1f187 Stefano Sabatini
    return 0;
104
}
105
106 bdab614b S.N. Hemanth Meenakshisundaram
AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
107 39135465 Vitor Sessak
{
108 c1d662fd Stefano Sabatini
    AVFilterFormats *ret = NULL;
109 bdab614b S.N. Hemanth Meenakshisundaram
    int fmt;
110 ad2c9501 S.N. Hemanth Meenakshisundaram
    int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB    :
111 5d6e4c16 Stefano Sabatini
                      type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
112 d3a4e41c Vitor Sessak
113 bdab614b S.N. Hemanth Meenakshisundaram
    for (fmt = 0; fmt < num_formats; fmt++)
114
        if ((type != AVMEDIA_TYPE_VIDEO) ||
115
            (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
116
            avfilter_add_format(&ret, fmt);
117 d3a4e41c Vitor Sessak
118
    return ret;
119 39135465 Vitor Sessak
}
120
121
void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
122
{
123
    *ref = f;
124
    f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
125
    f->refs[f->refcount-1] = ref;
126
}
127
128 30f4baeb Vitor Sessak
static int find_ref_index(AVFilterFormats **ref)
129 88cfb804 Vitor Sessak
{
130
    int i;
131
    for(i = 0; i < (*ref)->refcount; i ++)
132
        if((*ref)->refs[i] == ref)
133
            return i;
134
    return -1;
135
}
136
137 39135465 Vitor Sessak
void avfilter_formats_unref(AVFilterFormats **ref)
138
{
139 d3c01751 Stefano Sabatini
    int idx;
140
141 063e7692 Stefano Sabatini
    if (!*ref)
142
        return;
143
144 d3c01751 Stefano Sabatini
    idx = find_ref_index(ref);
145 88cfb804 Vitor Sessak
146 9189411b Vitor Sessak
    if(idx >= 0)
147 88cfb804 Vitor Sessak
        memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
148
            sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
149
150 39135465 Vitor Sessak
    if(!--(*ref)->refcount) {
151
        av_free((*ref)->formats);
152
        av_free((*ref)->refs);
153
        av_free(*ref);
154
    }
155
    *ref = NULL;
156
}
157
158 eac24950 Vitor Sessak
void avfilter_formats_changeref(AVFilterFormats **oldref,
159
                                AVFilterFormats **newref)
160
{
161 9189411b Vitor Sessak
    int idx = find_ref_index(oldref);
162 eac24950 Vitor Sessak
163 9189411b Vitor Sessak
    if(idx >= 0) {
164 eac24950 Vitor Sessak
        (*oldref)->refs[idx] = newref;
165
        *newref = *oldref;
166
        *oldref = NULL;
167
    }
168
}