FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cljr.c
Go to the documentation of this file.
1 /*
2  * Cirrus Logic AccuPak (CLJR) codec
3  * Copyright (c) 2003 Alex Beregszaszi
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * 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  * FFmpeg is distributed in the hope that it will be useful,
13  * 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  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Cirrus Logic AccuPak codec.
25  */
26 
27 #include "avcodec.h"
28 #include "libavutil/opt.h"
29 #include "get_bits.h"
30 #include "internal.h"
31 #include "put_bits.h"
32 
33 typedef struct CLJRContext {
37 } CLJRContext;
38 
40 {
41  CLJRContext * const a = avctx->priv_data;
42 
44  avctx->coded_frame = &a->picture;
45 
46  return 0;
47 }
48 
49 #if CONFIG_CLJR_DECODER
50 static int decode_frame(AVCodecContext *avctx,
51  void *data, int *got_frame,
52  AVPacket *avpkt)
53 {
54  const uint8_t *buf = avpkt->data;
55  int buf_size = avpkt->size;
56  CLJRContext * const a = avctx->priv_data;
57  GetBitContext gb;
58  AVFrame *picture = data;
59  AVFrame * const p = &a->picture;
60  int x, y, ret;
61 
62  if (p->data[0])
63  avctx->release_buffer(avctx, p);
64 
65  if (avctx->height <= 0 || avctx->width <= 0) {
66  av_log(avctx, AV_LOG_ERROR, "Invalid width or height\n");
67  return AVERROR_INVALIDDATA;
68  }
69 
70  if (buf_size / avctx->height < avctx->width) {
71  av_log(avctx, AV_LOG_ERROR,
72  "Resolution larger than buffer size. Invalid header?\n");
73  return AVERROR_INVALIDDATA;
74  }
75 
76  p->reference = 0;
77  if ((ret = ff_get_buffer(avctx, p)) < 0) {
78  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
79  return ret;
80  }
82  p->key_frame = 1;
83 
84  init_get_bits(&gb, buf, buf_size * 8);
85 
86  for (y = 0; y < avctx->height; y++) {
87  uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
88  uint8_t *cb = &a->picture.data[1][y * a->picture.linesize[1]];
89  uint8_t *cr = &a->picture.data[2][y * a->picture.linesize[2]];
90  for (x = 0; x < avctx->width; x += 4) {
91  luma[3] = (get_bits(&gb, 5)*33) >> 2;
92  luma[2] = (get_bits(&gb, 5)*33) >> 2;
93  luma[1] = (get_bits(&gb, 5)*33) >> 2;
94  luma[0] = (get_bits(&gb, 5)*33) >> 2;
95  luma += 4;
96  *(cb++) = get_bits(&gb, 6) << 2;
97  *(cr++) = get_bits(&gb, 6) << 2;
98  }
99  }
100 
101  *picture = a->picture;
102  *got_frame = 1;
103 
104  return buf_size;
105 }
106 
107 static av_cold int decode_init(AVCodecContext *avctx)
108 {
109  avctx->pix_fmt = AV_PIX_FMT_YUV411P;
110  return common_init(avctx);
111 }
112 
113 static av_cold int decode_end(AVCodecContext *avctx)
114 {
115  CLJRContext *a = avctx->priv_data;
116 
117  if (a->picture.data[0])
118  avctx->release_buffer(avctx, &a->picture);
119  return 0;
120 }
121 
122 AVCodec ff_cljr_decoder = {
123  .name = "cljr",
124  .type = AVMEDIA_TYPE_VIDEO,
125  .id = AV_CODEC_ID_CLJR,
126  .priv_data_size = sizeof(CLJRContext),
127  .init = decode_init,
128  .close = decode_end,
129  .decode = decode_frame,
130  .capabilities = CODEC_CAP_DR1,
131  .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
132 };
133 #endif
134 
135 #if CONFIG_CLJR_ENCODER
136 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
137  const AVFrame *p, int *got_packet)
138 {
139  CLJRContext *a = avctx->priv_data;
140  PutBitContext pb;
141  int x, y, ret;
142  uint32_t dither= avctx->frame_number;
143  static const uint32_t ordered_dither[2][2] =
144  {
145  { 0x10400000, 0x104F0000 },
146  { 0xCB2A0000, 0xCB250000 },
147  };
148 
149  if ((ret = ff_alloc_packet2(avctx, pkt, 32*avctx->height*avctx->width/4)) < 0)
150  return ret;
151 
153  avctx->coded_frame->key_frame = 1;
154 
155  init_put_bits(&pb, pkt->data, pkt->size);
156 
157  for (y = 0; y < avctx->height; y++) {
158  uint8_t *luma = &p->data[0][y * p->linesize[0]];
159  uint8_t *cb = &p->data[1][y * p->linesize[1]];
160  uint8_t *cr = &p->data[2][y * p->linesize[2]];
161  for (x = 0; x < avctx->width; x += 4) {
162  switch (a->dither_type) {
163  case 0: dither = 0x492A0000; break;
164  case 1: dither = dither * 1664525 + 1013904223; break;
165  case 2: dither = ordered_dither[ y&1 ][ (x>>2)&1 ];break;
166  }
167  put_bits(&pb, 5, (249*(luma[3] + (dither>>29) )) >> 11);
168  put_bits(&pb, 5, (249*(luma[2] + ((dither>>26)&7))) >> 11);
169  put_bits(&pb, 5, (249*(luma[1] + ((dither>>23)&7))) >> 11);
170  put_bits(&pb, 5, (249*(luma[0] + ((dither>>20)&7))) >> 11);
171  luma += 4;
172  put_bits(&pb, 6, (253*(*(cb++) + ((dither>>18)&3))) >> 10);
173  put_bits(&pb, 6, (253*(*(cr++) + ((dither>>16)&3))) >> 10);
174  }
175  }
176 
177  flush_put_bits(&pb);
178 
179  pkt->size = put_bits_count(&pb) / 8;
180  pkt->flags |= AV_PKT_FLAG_KEY;
181  *got_packet = 1;
182  return 0;
183 }
184 
185 #define OFFSET(x) offsetof(CLJRContext, x)
186 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
187 static const AVOption options[] = {
188  { "dither_type", "Dither type", OFFSET(dither_type), AV_OPT_TYPE_INT, { .i64=1 }, 0, 2, VE},
189  { NULL },
190 };
191 
192 static const AVClass class = {
193  .class_name = "cljr encoder",
194  .item_name = av_default_item_name,
195  .option = options,
196  .version = LIBAVUTIL_VERSION_INT,
197 };
198 
199 AVCodec ff_cljr_encoder = {
200  .name = "cljr",
201  .type = AVMEDIA_TYPE_VIDEO,
202  .id = AV_CODEC_ID_CLJR,
203  .priv_data_size = sizeof(CLJRContext),
204  .init = common_init,
205  .encode2 = encode_frame,
206  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV411P,
207  AV_PIX_FMT_NONE },
208  .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
209  .priv_class = &class,
210 };
211 #endif