FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
y41pdec.c
Go to the documentation of this file.
1 /*
2  * y41p decoder
3  *
4  * Copyright (c) 2012 Paul B Mahol
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "avcodec.h"
24 #include "internal.h"
25 
27 {
28  avctx->pix_fmt = AV_PIX_FMT_YUV411P;
29  avctx->bits_per_raw_sample = 12;
30 
31  if (avctx->width & 7) {
32  av_log(avctx, AV_LOG_WARNING, "y41p requires width to be divisible by 8.\n");
33  }
34 
36  if (!avctx->coded_frame) {
37  av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
38  return AVERROR(ENOMEM);
39  }
40 
41  return 0;
42 }
43 
44 static int y41p_decode_frame(AVCodecContext *avctx, void *data,
45  int *got_frame, AVPacket *avpkt)
46 {
47  AVFrame *pic = avctx->coded_frame;
48  uint8_t *src = avpkt->data;
49  uint8_t *y, *u, *v;
50  int i, j;
51 
52  if (pic->data[0])
53  avctx->release_buffer(avctx, pic);
54 
55  if (avpkt->size < 1.5 * avctx->height * avctx->width) {
56  av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
57  return AVERROR(EINVAL);
58  }
59 
60  pic->reference = 0;
61 
62  if (ff_get_buffer(avctx, pic) < 0) {
63  av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
64  return AVERROR(ENOMEM);
65  }
66 
67  pic->key_frame = 1;
69 
70  for (i = avctx->height - 1; i >= 0 ; i--) {
71  y = &pic->data[0][i * pic->linesize[0]];
72  u = &pic->data[1][i * pic->linesize[1]];
73  v = &pic->data[2][i * pic->linesize[2]];
74  for (j = 0; j < avctx->width; j += 8) {
75  *(u++) = *src++;
76  *(y++) = *src++;
77  *(v++) = *src++;
78  *(y++) = *src++;
79 
80  *(u++) = *src++;
81  *(y++) = *src++;
82  *(v++) = *src++;
83  *(y++) = *src++;
84 
85  *(y++) = *src++;
86  *(y++) = *src++;
87  *(y++) = *src++;
88  *(y++) = *src++;
89  }
90  }
91 
92  *got_frame = 1;
93  *(AVFrame *)data = *pic;
94 
95  return avpkt->size;
96 }
97 
99 {
100  if (avctx->coded_frame->data[0])
101  avctx->release_buffer(avctx, avctx->coded_frame);
102 
103  av_freep(&avctx->coded_frame);
104 
105  return 0;
106 }
107 
109  .name = "y41p",
110  .type = AVMEDIA_TYPE_VIDEO,
111  .id = AV_CODEC_ID_Y41P,
112  .init = y41p_decode_init,
113  .decode = y41p_decode_frame,
114  .close = y41p_decode_close,
115  .capabilities = CODEC_CAP_DR1,
116  .long_name = NULL_IF_CONFIG_SMALL("Uncompressed YUV 4:1:1 12-bit"),
117 };