summaryrefslogtreecommitdiff
path: root/libavutil/pixformaton.c
blob: 70e69f0a31c6dc2c9a3b96989c32f6a595896d91 (plain)
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
/*
 * Copyright (c) 2015 Kostya Shishkov
 * This file is part of Libav.
 *
 * Libav is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Libav is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.GPLv3.  If not see
 * <http://www.gnu.org/licenses/>.
 */

#include <string.h>

#include "avstring.h"
#include "buffer.h"
#include "pixformaton.h"
#include "mem.h"
#include "pixdesc.h"
#include "pixfmt.h"

typedef struct AVPixelFormatonRefInternal {
    AVPixelFormaton *pf;
    AVBufferRef *ref;
} AVPixelFormatonRefInternal;

AVPixelFormatonRef *av_pixformaton_alloc(void)
{
    AVPixelFormaton *pf;
    AVBufferRef *buf = NULL;

    AVPixelFormatonRefInternal *pref = NULL;

    buf = av_buffer_allocz(sizeof(*pf));
    if (!buf)
        return NULL;

    pref = av_mallocz(sizeof(*pref));
    if (!pref)
        goto fail;

    pf = (AVPixelFormaton*)buf->data;

    pref->pf  = pf;
    pref->ref = buf;

    return (AVPixelFormatonRef*)pref;
fail:
    av_buffer_unref(&buf);
    av_freep(&pref);
    return NULL;
}

AVPixelFormatonRef *av_pixformaton_ref(AVPixelFormatonRef *pf)
{
    AVPixelFormatonRefInternal *src = (AVPixelFormatonRefInternal*)pf;
    AVPixelFormatonRefInternal *dst;

    dst = av_mallocz(sizeof(*dst));
    if (!dst)
        return NULL;

    dst->ref = av_buffer_ref(src->ref);
    if (!dst->ref) {
        av_freep(&dst);
        return NULL;
    }

    dst->pf = src->pf;

    return (AVPixelFormatonRef*)dst;
}

void av_pixformaton_unref(AVPixelFormatonRef **pref)
{
    AVPixelFormatonRefInternal *pref_int = (AVPixelFormatonRefInternal*)*pref;

    if (!pref_int)
        return;

    av_buffer_unref(&pref_int->ref);
    av_freep(&pref_int);
}

AVPixelFormatonRef *av_pixformaton_from_pixfmt(enum AVPixelFormat pix_fmt)
{
    AVPixelFormaton *pf;
    AVPixelFormatonRef *pref;
    const AVPixFmtDescriptor *desc;

    int i;

    desc = av_pix_fmt_desc_get(pix_fmt);
    if (!desc)
        return NULL;

    pref = av_pixformaton_alloc();
    if (!pref)
        return NULL;

    pf = pref->pf;

    pf->flags = desc->flags;

    if (av_strstart(desc->name, "yuvj", NULL))
        pf->range = AVCOL_RANGE_JPEG;
    else
        pf->range = AVCOL_RANGE_UNSPECIFIED;

    pf->primaries = AVCOL_PRI_UNSPECIFIED;
    pf->transfer  = AVCOL_TRC_UNSPECIFIED;
    pf->space     = AVCOL_SPC_UNSPECIFIED;
    pf->location  = AVCHROMA_LOC_UNSPECIFIED;

    pf->nb_components = desc->nb_components;

    for (i = 0; i < pf->nb_components; i++) {
        AVPixelChromaton *chromaton = &pf->component_desc[i];
        const AVComponentDescriptor *comp = &desc->comp[i];

        chromaton->plane     = comp->plane;
        chromaton->next      = comp->step;
        chromaton->h_sub_log = desc->log2_chroma_w;
        chromaton->v_sub_log = desc->log2_chroma_h;
        chromaton->offset    = comp->offset;
        chromaton->shift     = comp->shift;
        chromaton->depth     = comp->depth;
        chromaton->packed    = 0; // XXX luzero does not remember
    }

    return pref;
}