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 #if CONFIG_CLJR_DECODER
34 static int decode_frame(AVCodecContext *avctx,
35  void *data, int *got_frame,
36  AVPacket *avpkt)
37 {
38  const uint8_t *buf = avpkt->data;
39  int buf_size = avpkt->size;
40  GetBitContext gb;
41  AVFrame * const p = data;
42  int x, y, ret;
43 
44  if (avctx->height <= 0 || avctx->width <= 0) {
45  av_log(avctx, AV_LOG_ERROR, "Invalid width or height\n");
46  return AVERROR_INVALIDDATA;
47  }
48 
49  if (buf_size / avctx->height < avctx->width) {
50  av_log(avctx, AV_LOG_ERROR,
51  "Resolution larger than buffer size. Invalid header?\n");
52  return AVERROR_INVALIDDATA;
53  }
54 
55  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
56  return ret;
58  p->key_frame = 1;
59 
60  init_get_bits(&gb, buf, buf_size * 8);
61 
62  for (y = 0; y < avctx->height; y++) {
63  uint8_t *luma = &p->data[0][y * p->linesize[0]];
64  uint8_t *cb = &p->data[1][y * p->linesize[1]];
65  uint8_t *cr = &p->data[2][y * p->linesize[2]];
66  for (x = 0; x < avctx->width; x += 4) {
67  luma[3] = (get_bits(&gb, 5)*33) >> 2;
68  luma[2] = (get_bits(&gb, 5)*33) >> 2;
69  luma[1] = (get_bits(&gb, 5)*33) >> 2;
70  luma[0] = (get_bits(&gb, 5)*33) >> 2;
71  luma += 4;
72  *(cb++) = get_bits(&gb, 6) << 2;
73  *(cr++) = get_bits(&gb, 6) << 2;
74  }
75  }
76 
77  *got_frame = 1;
78 
79  return buf_size;
80 }
81 
82 static av_cold int decode_init(AVCodecContext *avctx)
83 {
84  avctx->pix_fmt = AV_PIX_FMT_YUV411P;
85  return 0;
86 }
87 
88 AVCodec ff_cljr_decoder = {
89  .name = "cljr",
90  .type = AVMEDIA_TYPE_VIDEO,
91  .id = AV_CODEC_ID_CLJR,
92  .init = decode_init,
93  .decode = decode_frame,
94  .capabilities = CODEC_CAP_DR1,
95  .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
96 };
97 #endif
98 
99 #if CONFIG_CLJR_ENCODER
100 typedef struct CLJRContext {
101  AVClass *avclass;
102  AVFrame picture;
103  int dither_type;
104 } CLJRContext;
105 
106 static av_cold int encode_init(AVCodecContext *avctx)
107 {
108  CLJRContext * const a = avctx->priv_data;
109 
110  avctx->coded_frame = &a->picture;
111 
112  return 0;
113 }
114 
115 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
116  const AVFrame *p, int *got_packet)
117 {
118  CLJRContext *a = avctx->priv_data;
119  PutBitContext pb;
120  int x, y, ret;
121  uint32_t dither= avctx->frame_number;
122  static const uint32_t ordered_dither[2][2] =
123  {
124  { 0x10400000, 0x104F0000 },
125  { 0xCB2A0000, 0xCB250000 },
126  };
127 
128  if ((ret = ff_alloc_packet2(avctx, pkt, 32*avctx->height*avctx->width/4)) < 0)
129  return ret;
130 
132  avctx->coded_frame->key_frame = 1;
133 
134  init_put_bits(&pb, pkt->data, pkt->size);
135 
136  for (y = 0; y < avctx->height; y++) {
137  uint8_t *luma = &p->data[0][y * p->linesize[0]];
138  uint8_t *cb = &p->data[1][y * p->linesize[1]];
139  uint8_t *cr = &p->data[2][y * p->linesize[2]];
140  for (x = 0; x < avctx->width; x += 4) {
141  switch (a->dither_type) {
142  case 0: dither = 0x492A0000; break;
143  case 1: dither = dither * 1664525 + 1013904223; break;
144  case 2: dither = ordered_dither[ y&1 ][ (x>>2)&1 ];break;
145  }
146  put_bits(&pb, 5, (249*(luma[3] + (dither>>29) )) >> 11);
147  put_bits(&pb, 5, (249*(luma[2] + ((dither>>26)&7))) >> 11);
148  put_bits(&pb, 5, (249*(luma[1] + ((dither>>23)&7))) >> 11);
149  put_bits(&pb, 5, (249*(luma[0] + ((dither>>20)&7))) >> 11);
150  luma += 4;
151  put_bits(&pb, 6, (253*(*(cb++) + ((dither>>18)&3))) >> 10);
152  put_bits(&pb, 6, (253*(*(cr++) + ((dither>>16)&3))) >> 10);
153  }
154  }
155 
156  flush_put_bits(&pb);
157 
158  pkt->size = put_bits_count(&pb) / 8;
159  pkt->flags |= AV_PKT_FLAG_KEY;
160  *got_packet = 1;
161  return 0;
162 }
163 
164 #define OFFSET(x) offsetof(CLJRContext, x)
165 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
166 static const AVOption options[] = {
167  { "dither_type", "Dither type", OFFSET(dither_type), AV_OPT_TYPE_INT, { .i64=1 }, 0, 2, VE},
168  { NULL },
169 };
170 
171 static const AVClass class = {
172  .class_name = "cljr encoder",
173  .item_name = av_default_item_name,
174  .option = options,
175  .version = LIBAVUTIL_VERSION_INT,
176 };
177 
178 AVCodec ff_cljr_encoder = {
179  .name = "cljr",
180  .type = AVMEDIA_TYPE_VIDEO,
181  .id = AV_CODEC_ID_CLJR,
182  .priv_data_size = sizeof(CLJRContext),
183  .init = encode_init,
184  .encode2 = encode_frame,
185  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV411P,
186  AV_PIX_FMT_NONE },
187  .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
188  .priv_class = &class,
189 };
190 #endif
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
av_default_item_name
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
AVFrame * coded_frame
the picture in the bitstream
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:55
uint8_t
#define av_cold
Definition: attributes.h:78
AVOptions.
static AVPacket pkt
Definition: demuxing.c:56
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
static av_cold int decode_init(AVCodecContext *avctx)
bitstream reader API header.
const OptionDef options[]
Definition: ffserver.c:4697
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Discrete Time axis x
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
static void put_bits(J2kEncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
external API header
int flags
A combination of AV_PKT_FLAG values.
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:73
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
ret
Definition: avfilter.c:821
int width
picture width / height.
static av_cold int encode_init(AVCodecContext *avctx)
Definition: asvenc.c:238
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
int AC3_NAME() encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
NULL
Definition: eval.c:55
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
FIXME Range Coding of cb
Definition: snow.txt:367
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
Describe the class of an AVClass context structure.
Definition: log.h:50
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
#define OFFSET(x)
Definition: ffmpeg_opt.c:2553
#define VE
Definition: dnxhdenc.c:38
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:81
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:75
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:54
function y
Definition: D.m:1
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:139
int frame_number
Frame counter, set by libavcodec.
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:85
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
This structure stores compressed data.
bitstream writer API