comparison raw.c @ 1231:b88dfc4bbf8c libavcodec

* introducing new public interface in imgconvert.c + avcodec_get_pix_fmt converts textual representation of pixel format into the actual id. Complements avcodec_get_pix_fmt_name. + avpicture_layout serializes given picture into a flat array. Complements avpicture_fill. * adding a new option -pix_fmt to the ffmpeg, in order to control pixel format for the codecs that do support it, like rawvideo, for example. * reducing complexity of the rawvideo codec by splitting it in two and making it more reliable via hooking up to the avpicture_layout. Plus adding new FourCC as described here: http://www.fourcc.org * A tiny fix for avienc.c that makes avih and video strf consistent regarding codec FourCC.
author romansh
date Wed, 07 May 2003 19:01:45 +0000
parents f7522f310c7e
children ec946cb74397
comparison
equal deleted inserted replaced
1230:31d090bae36c 1231:b88dfc4bbf8c
22 * Raw Video Codec 22 * Raw Video Codec
23 */ 23 */
24 24
25 #include "avcodec.h" 25 #include "avcodec.h"
26 26
27 typedef struct RawVideoContext {
28 unsigned char * buffer; /* block of memory for holding one frame */
29 unsigned char * p; /* current position in buffer */
30 int length; /* number of bytes in buffer */
31 AVFrame pic; ///< AVCodecContext.coded_frame
32 } RawVideoContext;
27 33
28 typedef struct PixleFormatTag { 34 typedef struct PixleFormatTag {
29 int pix_fmt; 35 int pix_fmt;
30 unsigned int fourcc; 36 unsigned int fourcc;
31 } PixelFormatTag; 37 } PixelFormatTag;
32 38
33 const PixelFormatTag pixelFormatTags[] = { 39 const PixelFormatTag pixelFormatTags[] = {
34 { PIX_FMT_YUV422, MKTAG('Y', '4', '2', '2') }, 40 { PIX_FMT_YUV420P, MKTAG('I', '4', '2', '0') }, /* Planar formats */
35 { PIX_FMT_YUV420P, MKTAG('I', '4', '2', '0') }, 41 { PIX_FMT_YUV420P, MKTAG('I', 'Y', 'U', 'V') },
42 { PIX_FMT_YUV410P, MKTAG('Y', 'U', 'V', '9') },
43 { PIX_FMT_YUV411P, MKTAG('Y', '4', '1', 'B') },
44 { PIX_FMT_YUV422P, MKTAG('Y', '4', '2', 'B') },
45 { PIX_FMT_GRAY8, MKTAG('Y', '8', '0', '0') },
46 { PIX_FMT_GRAY8, MKTAG(' ', ' ', 'Y', '8') },
47
48
49 { PIX_FMT_YUV422, MKTAG('Y', '4', '2', '2') }, /* Packed formats */
50 { PIX_FMT_YUV422, MKTAG('U', 'Y', 'V', 'Y') },
51 { PIX_FMT_GRAY8, MKTAG('G', 'R', 'E', 'Y') },
52
36 { -1, 0 }, 53 { -1, 0 },
37 }; 54 };
38 55
39 static int findPixelFormat(unsigned int fourcc) 56 static int findPixelFormat(unsigned int fourcc)
40 { 57 {
45 tags++; 62 tags++;
46 } 63 }
47 return PIX_FMT_YUV420P; 64 return PIX_FMT_YUV420P;
48 } 65 }
49 66
67 static unsigned int findFourCC(int fmt)
68 {
69 const PixelFormatTag * tags = pixelFormatTags;
70 while (tags->pix_fmt >= 0) {
71 if (tags->pix_fmt == fmt)
72 return tags->fourcc;
73 tags++;
74 }
75 return 0;
76 }
50 77
51 typedef struct RawVideoContext { 78 /* RAW Decoder Implementation */
52 unsigned char * buffer; /* block of memory for holding one frame */
53 unsigned char * p; /* current position in buffer */
54 int length; /* number of bytes in buffer */
55 AVFrame pic; ///< AVCodecContext.coded_frame
56 } RawVideoContext;
57 79
58 80 static int raw_init_decoder(AVCodecContext *avctx)
59 static int raw_init(AVCodecContext *avctx)
60 { 81 {
61 RawVideoContext *context = avctx->priv_data; 82 RawVideoContext *context = avctx->priv_data;
62 83
63 if (avctx->codec_tag) { 84 if (avctx->codec_tag)
64 avctx->pix_fmt = findPixelFormat(avctx->codec_tag); 85 avctx->pix_fmt = findPixelFormat(avctx->codec_tag);
65 } 86
66 87 context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
67 context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height); 88 context->buffer = av_malloc(context->length);
68 context->buffer = av_malloc(context->length); 89 context->p = context->buffer;
69 context->p = context->buffer; 90 context->pic.pict_type = FF_I_TYPE;
70 91 context->pic.key_frame = 1;
71 context->pic.pict_type= FF_I_TYPE; 92
72 context->pic.key_frame= 1;
73 avctx->coded_frame= &context->pic; 93 avctx->coded_frame= &context->pic;
74 94
75 if (! context->buffer) { 95 if (!context->buffer)
76 return -1; 96 return -1;
77 } 97
78
79 return 0; 98 return 0;
80 } 99 }
81 100
82 static int raw_decode(AVCodecContext *avctx, 101 static int raw_decode(AVCodecContext *avctx,
83 void *data, int *data_size, 102 void *data, int *data_size,
108 avpicture_fill(picture, context->buffer, avctx->pix_fmt, avctx->width, avctx->height); 127 avpicture_fill(picture, context->buffer, avctx->pix_fmt, avctx->width, avctx->height);
109 *data_size = sizeof(AVPicture); 128 *data_size = sizeof(AVPicture);
110 return bytesNeeded; 129 return bytesNeeded;
111 } 130 }
112 131
113 static int raw_close(AVCodecContext *avctx) 132 static int raw_close_decoder(AVCodecContext *avctx)
114 { 133 {
115 RawVideoContext *context = avctx->priv_data; 134 RawVideoContext *context = avctx->priv_data;
135
136 av_freep(&context->buffer);
137 return 0;
138 }
116 139
117 av_freep(& context->buffer); 140 /* RAW Encoder Implementation */
118 141
142 static int raw_init_encoder(AVCodecContext *avctx)
143 {
144 avctx->coded_frame = (AVPicture*)avctx->priv_data;
145 avctx->coded_frame->pict_type = FF_I_TYPE;
146 avctx->coded_frame->key_frame = 1;
147 avctx->codec_tag = findFourCC(avctx->pix_fmt);
119 return 0; 148 return 0;
120 } 149 }
121 150
122 static int raw_encode(AVCodecContext *avctx, 151 static int raw_encode(AVCodecContext *avctx,
123 unsigned char *frame, int buf_size, void *data) 152 unsigned char *frame, int buf_size, void *data)
124 { 153 {
125 AVPicture * picture = data; 154 return avpicture_layout((AVPicture *)data, avctx->pix_fmt, avctx->width,
126 155 avctx->height, frame, buf_size);
127 unsigned char *src;
128 unsigned char *dest = frame;
129 int i, j;
130
131 int w = avctx->width;
132 int h = avctx->height;
133 int size = avpicture_get_size(avctx->pix_fmt, w, h);
134
135 if (size > buf_size) {
136 return -1;
137 }
138
139 switch(avctx->pix_fmt) {
140 case PIX_FMT_YUV420P:
141 for(i=0;i<3;i++) {
142 if (i == 1) {
143 w >>= 1;
144 h >>= 1;
145 }
146 src = picture->data[i];
147 for(j=0;j<h;j++) {
148 memcpy(dest, src, w);
149 dest += w;
150 src += picture->linesize[i];
151 }
152 }
153 break;
154 case PIX_FMT_YUV422P:
155 for(i=0;i<3;i++) {
156 if (i == 1) {
157 w >>= 1;
158 }
159 src = picture->data[i];
160 for(j=0;j<h;j++) {
161 memcpy(dest, src, w);
162 dest += w;
163 src += picture->linesize[i];
164 }
165 }
166 break;
167 case PIX_FMT_YUV444P:
168 for(i=0;i<3;i++) {
169 src = picture->data[i];
170 for(j=0;j<h;j++) {
171 memcpy(dest, src, w);
172 dest += w;
173 src += picture->linesize[i];
174 }
175 }
176 break;
177 case PIX_FMT_YUV422:
178 src = picture->data[0];
179 for(j=0;j<h;j++) {
180 memcpy(dest, src, w * 2);
181 dest += w * 2;
182 src += picture->linesize[0];
183 }
184 break;
185 case PIX_FMT_RGB24:
186 case PIX_FMT_BGR24:
187 src = picture->data[0];
188 for(j=0;j<h;j++) {
189 memcpy(dest, src, w * 3);
190 dest += w * 3;
191 src += picture->linesize[0];
192 }
193 break;
194 default:
195 return -1;
196 }
197
198 return size;
199 } 156 }
200 157
158 AVCodec rawvideo_encoder = {
159 "rawvideo",
160 CODEC_TYPE_VIDEO,
161 CODEC_ID_RAWVIDEO,
162 sizeof(AVFrame),
163 raw_init_encoder,
164 raw_encode,
165 };
201 166
202 AVCodec rawvideo_codec = { 167 AVCodec rawvideo_decoder = {
203 "rawvideo", 168 "rawvideo",
204 CODEC_TYPE_VIDEO, 169 CODEC_TYPE_VIDEO,
205 CODEC_ID_RAWVIDEO, 170 CODEC_ID_RAWVIDEO,
206 sizeof(RawVideoContext), 171 sizeof(RawVideoContext),
207 raw_init, 172 raw_init_decoder,
208 raw_encode, 173 NULL,
209 raw_close, 174 raw_close_decoder,
210 raw_decode, 175 raw_decode,
211 }; 176 };