Libav
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 #if CONFIG_CLJR_DECODER
33 static int decode_frame(AVCodecContext *avctx,
34  void *data, int *got_frame,
35  AVPacket *avpkt)
36 {
37  const uint8_t *buf = avpkt->data;
38  int buf_size = avpkt->size;
39  GetBitContext gb;
40  AVFrame * const p = data;
41  int x, y, ret;
42 
43  if (avctx->height <= 0 || avctx->width <= 0) {
44  av_log(avctx, AV_LOG_ERROR, "Invalid width or height\n");
45  return AVERROR_INVALIDDATA;
46  }
47 
48  if (buf_size < avctx->height * avctx->width) {
49  av_log(avctx, AV_LOG_ERROR,
50  "Resolution larger than buffer size. Invalid header?\n");
51  return AVERROR_INVALIDDATA;
52  }
53 
54  if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
55  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
56  return ret;
57  }
59  p->key_frame = 1;
60 
61  init_get_bits(&gb, buf, buf_size * 8);
62 
63  for (y = 0; y < avctx->height; y++) {
64  uint8_t *luma = &p->data[0][y * p->linesize[0]];
65  uint8_t *cb = &p->data[1][y * p->linesize[1]];
66  uint8_t *cr = &p->data[2][y * p->linesize[2]];
67  for (x = 0; x < avctx->width; x += 4) {
68  luma[3] = get_bits(&gb, 5) << 3;
69  luma[2] = get_bits(&gb, 5) << 3;
70  luma[1] = get_bits(&gb, 5) << 3;
71  luma[0] = get_bits(&gb, 5) << 3;
72  luma += 4;
73  *(cb++) = get_bits(&gb, 6) << 2;
74  *(cr++) = get_bits(&gb, 6) << 2;
75  }
76  }
77 
78  *got_frame = 1;
79 
80  return buf_size;
81 }
82 
83 static av_cold int decode_init(AVCodecContext *avctx)
84 {
85  avctx->pix_fmt = AV_PIX_FMT_YUV411P;
86  return 0;
87 }
88 
89 AVCodec ff_cljr_decoder = {
90  .name = "cljr",
91  .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
92  .type = AVMEDIA_TYPE_VIDEO,
93  .id = AV_CODEC_ID_CLJR,
94  .init = decode_init,
95  .decode = decode_frame,
96  .capabilities = CODEC_CAP_DR1,
97 };
98 #endif
99 
100 #if CONFIG_CLJR_ENCODER
101 static av_cold int encode_init(AVCodecContext *avctx)
102 {
103  avctx->coded_frame = av_frame_alloc();
104  if (!avctx->coded_frame)
105  return AVERROR(ENOMEM);
106 
107  return 0;
108 }
109 
110 static av_cold int encode_close(AVCodecContext *avctx)
111 {
112  av_frame_free(&avctx->coded_frame);
113  return 0;
114 }
115 
116 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
117  const AVFrame *p, int *got_packet)
118 {
119  PutBitContext pb;
120  int x, y, ret;
121 
122  if ((ret = ff_alloc_packet(pkt, 32*avctx->height*avctx->width/4)) < 0) {
123  av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
124  return ret;
125  }
126 
128  avctx->coded_frame->key_frame = 1;
129 
130  init_put_bits(&pb, pkt->data, pkt->size);
131 
132  for (y = 0; y < avctx->height; y++) {
133  uint8_t *luma = &p->data[0][y * p->linesize[0]];
134  uint8_t *cb = &p->data[1][y * p->linesize[1]];
135  uint8_t *cr = &p->data[2][y * p->linesize[2]];
136  for (x = 0; x < avctx->width; x += 4) {
137  put_bits(&pb, 5, luma[3] >> 3);
138  put_bits(&pb, 5, luma[2] >> 3);
139  put_bits(&pb, 5, luma[1] >> 3);
140  put_bits(&pb, 5, luma[0] >> 3);
141  luma += 4;
142  put_bits(&pb, 6, *(cb++) >> 2);
143  put_bits(&pb, 6, *(cr++) >> 2);
144  }
145  }
146 
147  flush_put_bits(&pb);
148 
149  pkt->size = put_bits_count(&pb) / 8;
150  pkt->flags |= AV_PKT_FLAG_KEY;
151  *got_packet = 1;
152  return 0;
153 }
154 
155 AVCodec ff_cljr_encoder = {
156  .name = "cljr",
157  .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
158  .type = AVMEDIA_TYPE_VIDEO,
159  .id = AV_CODEC_ID_CLJR,
160  .init = encode_init,
161  .encode2 = encode_frame,
162  .close = encode_close,
163  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV411P,
164  AV_PIX_FMT_NONE },
165 };
166 #endif
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
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
Definition: avcodec.h:2506
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
AVCodec.
Definition: avcodec.h:2755
static av_cold int encode_close(AVCodecContext *avctx)
Definition: dpxenc.c:178
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:43
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:711
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:973
static av_cold int decode_init(AVCodecContext *avctx)
Definition: 4xm.c:932
bitstream reader API header.
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: 4xm.c:787
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1023
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:55
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:2762
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:139
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:72
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:168
int width
picture width / height.
Definition: avcodec.h:1217
static av_cold int encode_init(AVCodecContext *avctx)
Definition: asvenc.c:237
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1125
int AC3_NAME() encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:125
main external API structure.
Definition: avcodec.h:1054
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:575
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:113
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:88
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:53
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:163
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:950
bitstream writer API