~ubuntu-branches/ubuntu/trusty/gst-libav1.0/trusty-proposed

« back to all changes in this revision

Viewing changes to gst-libs/ext/libav/libavcodec/vcr1.c

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2013-09-24 17:07:00 UTC
  • mfrom: (1.1.17) (7.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20130924170700-4dg62s3pwl0pdakz
Tags: 1.2.0-1
* New upstream stable release:
  + debian/control:
    - Build depend on GStreamer and gst-plugins-base >= 1.2.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
/**
23
23
 * @file
24
 
 * ati vcr1 codec.
 
24
 * ATI VCR1 codec
25
25
 */
26
26
 
27
27
#include "avcodec.h"
28
28
#include "dsputil.h"
29
 
 
30
 
//#undef NDEBUG
31
 
//#include <assert.h>
32
 
 
33
 
/* Disable the encoder. */
34
 
#undef CONFIG_VCR1_ENCODER
35
 
#define CONFIG_VCR1_ENCODER 0
36
 
 
37
 
typedef struct VCR1Context{
38
 
    AVCodecContext *avctx;
 
29
#include "internal.h"
 
30
#include "libavutil/internal.h"
 
31
 
 
32
typedef struct VCR1Context {
39
33
    AVFrame picture;
40
34
    int delta[16];
41
35
    int offset[4];
42
36
} VCR1Context;
43
37
 
44
 
static int decode_frame(AVCodecContext *avctx,
45
 
                        void *data, int *data_size,
46
 
                        AVPacket *avpkt)
47
 
{
48
 
    const uint8_t *buf = avpkt->data;
49
 
    int buf_size = avpkt->size;
50
 
    VCR1Context * const a = avctx->priv_data;
51
 
    AVFrame *picture = data;
52
 
    AVFrame * const p= (AVFrame*)&a->picture;
53
 
    const uint8_t *bytestream= buf;
 
38
static av_cold int vcr1_common_init(AVCodecContext *avctx)
 
39
{
 
40
    VCR1Context *const a = avctx->priv_data;
 
41
 
 
42
    avctx->coded_frame = &a->picture;
 
43
 
 
44
    return 0;
 
45
}
 
46
 
 
47
static av_cold int vcr1_decode_init(AVCodecContext *avctx)
 
48
{
 
49
    vcr1_common_init(avctx);
 
50
 
 
51
    avctx->pix_fmt = AV_PIX_FMT_YUV410P;
 
52
 
 
53
    if (avctx->width & 7) {
 
54
        av_log(avctx, AV_LOG_ERROR, "Width %d is not divisble by 8.\n", avctx->width);
 
55
        return AVERROR_INVALIDDATA;
 
56
    }
 
57
 
 
58
    return 0;
 
59
}
 
60
 
 
61
static av_cold int vcr1_decode_end(AVCodecContext *avctx)
 
62
{
 
63
    VCR1Context *s = avctx->priv_data;
 
64
 
 
65
    if (s->picture.data[0])
 
66
        avctx->release_buffer(avctx, &s->picture);
 
67
 
 
68
    return 0;
 
69
}
 
70
 
 
71
static int vcr1_decode_frame(AVCodecContext *avctx, void *data,
 
72
                             int *got_frame, AVPacket *avpkt)
 
73
{
 
74
    const uint8_t *buf        = avpkt->data;
 
75
    int buf_size              = avpkt->size;
 
76
    VCR1Context *const a      = avctx->priv_data;
 
77
    AVFrame *picture          = data;
 
78
    AVFrame *const p          = &a->picture;
 
79
    const uint8_t *bytestream = buf;
54
80
    int i, x, y;
55
81
 
56
 
    if(p->data[0])
 
82
    if (p->data[0])
57
83
        avctx->release_buffer(avctx, p);
58
84
 
59
 
    p->reference= 0;
60
 
    if(avctx->get_buffer(avctx, p) < 0){
 
85
    p->reference = 0;
 
86
    if (ff_get_buffer(avctx, p) < 0) {
61
87
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
62
88
        return -1;
63
89
    }
64
 
    p->pict_type= AV_PICTURE_TYPE_I;
65
 
    p->key_frame= 1;
66
 
 
67
 
    for(i=0; i<16; i++){
68
 
        a->delta[i]= *(bytestream++);
 
90
    p->pict_type = AV_PICTURE_TYPE_I;
 
91
    p->key_frame = 1;
 
92
 
 
93
    if (buf_size < 32)
 
94
        goto packet_small;
 
95
 
 
96
    for (i = 0; i < 16; i++) {
 
97
        a->delta[i] = *bytestream++;
69
98
        bytestream++;
 
99
        buf_size--;
70
100
    }
71
101
 
72
 
    for(y=0; y<avctx->height; y++){
 
102
    for (y = 0; y < avctx->height; y++) {
73
103
        int offset;
74
 
        uint8_t *luma= &a->picture.data[0][ y*a->picture.linesize[0] ];
75
 
 
76
 
        if((y&3) == 0){
77
 
            uint8_t *cb= &a->picture.data[1][ (y>>2)*a->picture.linesize[1] ];
78
 
            uint8_t *cr= &a->picture.data[2][ (y>>2)*a->picture.linesize[2] ];
79
 
 
80
 
            for(i=0; i<4; i++)
81
 
                a->offset[i]= *(bytestream++);
82
 
 
83
 
            offset= a->offset[0] - a->delta[ bytestream[2]&0xF ];
84
 
            for(x=0; x<avctx->width; x+=4){
85
 
                luma[0]=( offset += a->delta[ bytestream[2]&0xF ]);
86
 
                luma[1]=( offset += a->delta[ bytestream[2]>>4  ]);
87
 
                luma[2]=( offset += a->delta[ bytestream[0]&0xF ]);
88
 
                luma[3]=( offset += a->delta[ bytestream[0]>>4  ]);
89
 
                luma += 4;
90
 
 
91
 
                *(cb++) = bytestream[3];
92
 
                *(cr++) = bytestream[1];
93
 
 
94
 
                bytestream+= 4;
 
104
        uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
 
105
 
 
106
        if ((y & 3) == 0) {
 
107
            uint8_t *cb = &a->picture.data[1][(y >> 2) * a->picture.linesize[1]];
 
108
            uint8_t *cr = &a->picture.data[2][(y >> 2) * a->picture.linesize[2]];
 
109
 
 
110
            if (buf_size < 4 + avctx->width)
 
111
                goto packet_small;
 
112
 
 
113
            for (i = 0; i < 4; i++)
 
114
                a->offset[i] = *bytestream++;
 
115
            buf_size -= 4;
 
116
 
 
117
            offset = a->offset[0] - a->delta[bytestream[2] & 0xF];
 
118
            for (x = 0; x < avctx->width; x += 4) {
 
119
                luma[0]     = offset += a->delta[bytestream[2] & 0xF];
 
120
                luma[1]     = offset += a->delta[bytestream[2] >>  4];
 
121
                luma[2]     = offset += a->delta[bytestream[0] & 0xF];
 
122
                luma[3]     = offset += a->delta[bytestream[0] >>  4];
 
123
                luma       += 4;
 
124
 
 
125
                *cb++       = bytestream[3];
 
126
                *cr++       = bytestream[1];
 
127
 
 
128
                bytestream += 4;
 
129
                buf_size   -= 4;
95
130
            }
96
 
        }else{
97
 
            offset= a->offset[y&3] - a->delta[ bytestream[2]&0xF ];
98
 
 
99
 
            for(x=0; x<avctx->width; x+=8){
100
 
                luma[0]=( offset += a->delta[ bytestream[2]&0xF ]);
101
 
                luma[1]=( offset += a->delta[ bytestream[2]>>4  ]);
102
 
                luma[2]=( offset += a->delta[ bytestream[3]&0xF ]);
103
 
                luma[3]=( offset += a->delta[ bytestream[3]>>4  ]);
104
 
                luma[4]=( offset += a->delta[ bytestream[0]&0xF ]);
105
 
                luma[5]=( offset += a->delta[ bytestream[0]>>4  ]);
106
 
                luma[6]=( offset += a->delta[ bytestream[1]&0xF ]);
107
 
                luma[7]=( offset += a->delta[ bytestream[1]>>4  ]);
108
 
                luma += 8;
109
 
                bytestream+= 4;
 
131
        } else {
 
132
            if (buf_size < avctx->width / 2)
 
133
                goto packet_small;
 
134
 
 
135
            offset = a->offset[y & 3] - a->delta[bytestream[2] & 0xF];
 
136
 
 
137
            for (x = 0; x < avctx->width; x += 8) {
 
138
                luma[0]     = offset += a->delta[bytestream[2] & 0xF];
 
139
                luma[1]     = offset += a->delta[bytestream[2] >>  4];
 
140
                luma[2]     = offset += a->delta[bytestream[3] & 0xF];
 
141
                luma[3]     = offset += a->delta[bytestream[3] >>  4];
 
142
                luma[4]     = offset += a->delta[bytestream[0] & 0xF];
 
143
                luma[5]     = offset += a->delta[bytestream[0] >>  4];
 
144
                luma[6]     = offset += a->delta[bytestream[1] & 0xF];
 
145
                luma[7]     = offset += a->delta[bytestream[1] >>  4];
 
146
                luma       += 8;
 
147
                bytestream += 4;
 
148
                buf_size   -= 4;
110
149
            }
111
150
        }
112
151
    }
113
152
 
114
 
    *picture= *(AVFrame*)&a->picture;
115
 
    *data_size = sizeof(AVPicture);
 
153
    *picture   = a->picture;
 
154
    *got_frame = 1;
116
155
 
117
156
    return buf_size;
 
157
packet_small:
 
158
    av_log(avctx, AV_LOG_ERROR, "Input packet too small.\n");
 
159
    return AVERROR_INVALIDDATA;
118
160
}
119
161
 
 
162
AVCodec ff_vcr1_decoder = {
 
163
    .name           = "vcr1",
 
164
    .type           = AVMEDIA_TYPE_VIDEO,
 
165
    .id             = AV_CODEC_ID_VCR1,
 
166
    .priv_data_size = sizeof(VCR1Context),
 
167
    .init           = vcr1_decode_init,
 
168
    .close          = vcr1_decode_end,
 
169
    .decode         = vcr1_decode_frame,
 
170
    .capabilities   = CODEC_CAP_DR1,
 
171
    .long_name      = NULL_IF_CONFIG_SMALL("ATI VCR1"),
 
172
};
 
173
 
 
174
/* Disable the encoder. */
 
175
#undef CONFIG_VCR1_ENCODER
 
176
#define CONFIG_VCR1_ENCODER 0
 
177
 
120
178
#if CONFIG_VCR1_ENCODER
121
 
static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
122
 
    VCR1Context * const a = avctx->priv_data;
123
 
    AVFrame *pict = data;
124
 
    AVFrame * const p= (AVFrame*)&a->picture;
 
179
 
 
180
#include "put_bits.h"
 
181
 
 
182
static int vcr1_encode_frame(AVCodecContext *avctx, unsigned char *buf,
 
183
                             int buf_size, void *data)
 
184
{
 
185
    VCR1Context *const a = avctx->priv_data;
 
186
    AVFrame *pict        = data;
 
187
    AVFrame *const p     = &a->picture;
125
188
    int size;
126
189
 
127
 
    *p = *pict;
128
 
    p->pict_type= AV_PICTURE_TYPE_I;
129
 
    p->key_frame= 1;
 
190
    *p           = *pict;
 
191
    p->pict_type = AV_PICTURE_TYPE_I;
 
192
    p->key_frame = 1;
130
193
 
131
194
    avpriv_align_put_bits(&a->pb);
132
 
    while(get_bit_count(&a->pb)&31)
133
 
        put_bits(&a->pb, 8, 0);
134
 
 
135
 
    size= get_bit_count(&a->pb)/32;
136
 
 
137
 
    return size*4;
138
 
}
139
 
#endif
140
 
 
141
 
static av_cold void common_init(AVCodecContext *avctx){
142
 
    VCR1Context * const a = avctx->priv_data;
143
 
 
144
 
    avctx->coded_frame= (AVFrame*)&a->picture;
145
 
    a->avctx= avctx;
146
 
}
147
 
 
148
 
static av_cold int decode_init(AVCodecContext *avctx){
149
 
 
150
 
    common_init(avctx);
151
 
 
152
 
    avctx->pix_fmt= PIX_FMT_YUV410P;
153
 
 
154
 
    return 0;
155
 
}
156
 
 
157
 
static av_cold int decode_end(AVCodecContext *avctx){
158
 
    VCR1Context *s = avctx->priv_data;
159
 
 
160
 
    if (s->picture.data[0])
161
 
        avctx->release_buffer(avctx, &s->picture);
162
 
 
163
 
    return 0;
164
 
}
165
 
 
166
 
#if CONFIG_VCR1_ENCODER
167
 
static av_cold int encode_init(AVCodecContext *avctx){
168
 
 
169
 
    common_init(avctx);
170
 
 
171
 
    return 0;
172
 
}
173
 
#endif
174
 
 
175
 
AVCodec ff_vcr1_decoder = {
176
 
    .name           = "vcr1",
177
 
    .type           = AVMEDIA_TYPE_VIDEO,
178
 
    .id             = CODEC_ID_VCR1,
179
 
    .priv_data_size = sizeof(VCR1Context),
180
 
    .init           = decode_init,
181
 
    .close          = decode_end,
182
 
    .decode         = decode_frame,
183
 
    .capabilities   = CODEC_CAP_DR1,
184
 
    .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
185
 
};
186
 
 
187
 
#if CONFIG_VCR1_ENCODER
 
195
    flush_put_bits(&a->pb);
 
196
 
 
197
    size = put_bits_count(&a->pb) / 32;
 
198
 
 
199
    return size * 4;
 
200
}
 
201
 
188
202
AVCodec ff_vcr1_encoder = {
189
203
    .name           = "vcr1",
190
204
    .type           = AVMEDIA_TYPE_VIDEO,
191
 
    .id             = CODEC_ID_VCR1,
 
205
    .id             = AV_CODEC_ID_VCR1,
192
206
    .priv_data_size = sizeof(VCR1Context),
193
 
    .init           = encode_init,
194
 
    .encode         = encode_frame,
195
 
    .long_name = NULL_IF_CONFIG_SMALL("ATI VCR1"),
 
207
    .init           = vcr1_common_init,
 
208
    .encode         = vcr1_encode_frame,
 
209
    .long_name      = NULL_IF_CONFIG_SMALL("ATI VCR1"),
196
210
};
197
 
#endif
 
211
#endif /* CONFIG_VCR1_ENCODER */