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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "avcodec.h"
28 #include "get_bits.h"
29 #include "internal.h"
30 #include "put_bits.h"
31 
32 typedef struct CLJRContext {
34 } CLJRContext;
35 
37 {
38  CLJRContext * const a = avctx->priv_data;
39 
40  avctx->coded_frame = &a->picture;
41 
42  return 0;
43 }
44 
45 #if CONFIG_CLJR_DECODER
46 static int decode_frame(AVCodecContext *avctx,
47  void *data, int *got_frame,
48  AVPacket *avpkt)
49 {
50  const uint8_t *buf = avpkt->data;
51  int buf_size = avpkt->size;
52  CLJRContext * const a = avctx->priv_data;
53  GetBitContext gb;
54  AVFrame *picture = data;
55  AVFrame * const p = &a->picture;
56  int x, y;
57 
58  if (p->data[0])
59  avctx->release_buffer(avctx, p);
60 
61  if (avctx->height <= 0 || avctx->width <= 0) {
62  av_log(avctx, AV_LOG_ERROR, "Invalid width or height\n");
63  return AVERROR_INVALIDDATA;
64  }
65 
66  if (buf_size < avctx->height * avctx->width) {
67  av_log(avctx, AV_LOG_ERROR,
68  "Resolution larger than buffer size. Invalid header?\n");
69  return AVERROR_INVALIDDATA;
70  }
71 
72  p->reference = 0;
73  if (ff_get_buffer(avctx, p) < 0) {
74  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
75  return -1;
76  }
78  p->key_frame = 1;
79 
80  init_get_bits(&gb, buf, buf_size * 8);
81 
82  for (y = 0; y < avctx->height; y++) {
83  uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
84  uint8_t *cb = &a->picture.data[1][y * a->picture.linesize[1]];
85  uint8_t *cr = &a->picture.data[2][y * a->picture.linesize[2]];
86  for (x = 0; x < avctx->width; x += 4) {
87  luma[3] = get_bits(&gb, 5) << 3;
88  luma[2] = get_bits(&gb, 5) << 3;
89  luma[1] = get_bits(&gb, 5) << 3;
90  luma[0] = get_bits(&gb, 5) << 3;
91  luma += 4;
92  *(cb++) = get_bits(&gb, 6) << 2;
93  *(cr++) = get_bits(&gb, 6) << 2;
94  }
95  }
96 
97  *picture = a->picture;
98  *got_frame = 1;
99 
100  return buf_size;
101 }
102 
103 static av_cold int decode_init(AVCodecContext *avctx)
104 {
105  avctx->pix_fmt = AV_PIX_FMT_YUV411P;
106  return common_init(avctx);
107 }
108 
109 static av_cold int decode_end(AVCodecContext *avctx)
110 {
111  CLJRContext *a = avctx->priv_data;
112 
113  if (a->picture.data[0])
114  avctx->release_buffer(avctx, &a->picture);
115  return 0;
116 }
117 
118 AVCodec ff_cljr_decoder = {
119  .name = "cljr",
120  .type = AVMEDIA_TYPE_VIDEO,
121  .id = AV_CODEC_ID_CLJR,
122  .priv_data_size = sizeof(CLJRContext),
123  .init = decode_init,
124  .close = decode_end,
125  .decode = decode_frame,
126  .capabilities = CODEC_CAP_DR1,
127  .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
128 };
129 #endif
130 
131 #if CONFIG_CLJR_ENCODER
132 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
133  const AVFrame *p, int *got_packet)
134 {
135  PutBitContext pb;
136  int x, y, ret;
137 
138  if ((ret = ff_alloc_packet(pkt, 32*avctx->height*avctx->width/4)) < 0) {
139  av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
140  return ret;
141  }
142 
144  avctx->coded_frame->key_frame = 1;
145 
146  init_put_bits(&pb, pkt->data, pkt->size);
147 
148  for (y = 0; y < avctx->height; y++) {
149  uint8_t *luma = &p->data[0][y * p->linesize[0]];
150  uint8_t *cb = &p->data[1][y * p->linesize[1]];
151  uint8_t *cr = &p->data[2][y * p->linesize[2]];
152  for (x = 0; x < avctx->width; x += 4) {
153  put_bits(&pb, 5, luma[3] >> 3);
154  put_bits(&pb, 5, luma[2] >> 3);
155  put_bits(&pb, 5, luma[1] >> 3);
156  put_bits(&pb, 5, luma[0] >> 3);
157  luma += 4;
158  put_bits(&pb, 6, *(cb++) >> 2);
159  put_bits(&pb, 6, *(cr++) >> 2);
160  }
161  }
162 
163  flush_put_bits(&pb);
164 
165  pkt->size = put_bits_count(&pb) / 8;
166  pkt->flags |= AV_PKT_FLAG_KEY;
167  *got_packet = 1;
168  return 0;
169 }
170 
171 AVCodec ff_cljr_encoder = {
172  .name = "cljr",
173  .type = AVMEDIA_TYPE_VIDEO,
174  .id = AV_CODEC_ID_CLJR,
175  .priv_data_size = sizeof(CLJRContext),
176  .init = common_init,
177  .encode2 = encode_frame,
178  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV411P,
179  AV_PIX_FMT_NONE },
180  .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
181 };
182 #endif
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
AVCodec.
Definition: avcodec.h:2960
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
uint8_t
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static av_cold int decode_init(AVCodecContext *avctx)
Definition: 4xm.c:948
bitstream reader API header.
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: 4xm.c:800
struct CLJRContext CLJRContext
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1132
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:136
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:921
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:70
static AVFrame * picture
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
int width
picture width / height.
Definition: avcodec.h:1508
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
AVFrame picture
Definition: cljr.c:33
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:878
static av_cold int common_init(AVCodecContext *avctx)
Definition: cljr.c:36
int AC3_NAME() encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
static av_cold int decode_end(AVCodecContext *avctx)
Definition: 4xm.c:970
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int height
Definition: gxfenc.c:72
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:86
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:72
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:52
void * priv_data
Definition: avcodec.h:1382
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:898
bitstream writer API