FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
frwu.c
Go to the documentation of this file.
1 /*
2  * Forward Uncompressed
3  *
4  * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
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 "bytestream.h"
25 #include "internal.h"
26 #include "libavutil/opt.h"
27 
28 typedef struct {
31 } FRWUContext;
32 
34 {
35  if (avctx->width & 1) {
36  av_log(avctx, AV_LOG_ERROR, "frwu needs even width\n");
37  return AVERROR(EINVAL);
38  }
39  avctx->pix_fmt = AV_PIX_FMT_UYVY422;
40 
42  if (!avctx->coded_frame)
43  return AVERROR(ENOMEM);
44 
45  return 0;
46 }
47 
48 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
49  AVPacket *avpkt)
50 {
51  FRWUContext *s = avctx->priv_data;
52  int field, ret;
53  AVFrame *pic = avctx->coded_frame;
54  const uint8_t *buf = avpkt->data;
55  const uint8_t *buf_end = buf + avpkt->size;
56 
57  if (pic->data[0])
58  avctx->release_buffer(avctx, pic);
59 
60  if (avpkt->size < avctx->width * 2 * avctx->height + 4 + 2*8) {
61  av_log(avctx, AV_LOG_ERROR, "Packet is too small.\n");
62  return AVERROR_INVALIDDATA;
63  }
64  if (bytestream_get_le32(&buf) != MKTAG('F', 'R', 'W', '1')) {
65  av_log(avctx, AV_LOG_ERROR, "incorrect marker\n");
66  return AVERROR_INVALIDDATA;
67  }
68 
69  pic->reference = 0;
70  if ((ret = ff_get_buffer(avctx, pic)) < 0) {
71  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
72  return ret;
73  }
74 
76  pic->key_frame = 1;
77 
78  for (field = 0; field < 2; field++) {
79  int i;
80  int field_h = (avctx->height + !field) >> 1;
81  int field_size, min_field_size = avctx->width * 2 * field_h;
82  uint8_t *dst = pic->data[0];
83  if (buf_end - buf < 8)
84  return AVERROR_INVALIDDATA;
85  buf += 4; // flags? 0x80 == bottom field maybe?
86  field_size = bytestream_get_le32(&buf);
87  if (field_size < min_field_size) {
88  av_log(avctx, AV_LOG_ERROR, "Field size %i is too small (required %i)\n", field_size, min_field_size);
89  return AVERROR_INVALIDDATA;
90  }
91  if (buf_end - buf < field_size) {
92  av_log(avctx, AV_LOG_ERROR, "Packet is too small, need %i, have %i\n", field_size, (int)(buf_end - buf));
93  return AVERROR_INVALIDDATA;
94  }
95  if (field ^ s->change_field_order) {
96  dst += pic->linesize[0];
97  } else if (s->change_field_order) {
98  dst += 2 * pic->linesize[0];
99  }
100  for (i = 0; i < field_h; i++) {
101  if (s->change_field_order && field && i == field_h - 1)
102  dst = pic->data[0];
103  memcpy(dst, buf, avctx->width * 2);
104  buf += avctx->width * 2;
105  dst += pic->linesize[0] << 1;
106  }
107  buf += field_size - min_field_size;
108  }
109 
110  *got_frame = 1;
111  *(AVFrame*)data = *pic;
112 
113  return avpkt->size;
114 }
115 
117 {
118  AVFrame *pic = avctx->coded_frame;
119  if (pic->data[0])
120  avctx->release_buffer(avctx, pic);
121  av_freep(&avctx->coded_frame);
122 
123  return 0;
124 }
125 
126 static const AVOption frwu_options[] = {
127  {"change_field_order", "Change field order", offsetof(FRWUContext, change_field_order), FF_OPT_TYPE_INT,
129  {NULL}
130 };
131 
132 static const AVClass frwu_class = {
133  .class_name = "frwu Decoder",
134  .item_name = av_default_item_name,
135  .option = frwu_options,
136  .version = LIBAVUTIL_VERSION_INT,
137 };
138 
140  .name = "frwu",
141  .type = AVMEDIA_TYPE_VIDEO,
142  .id = AV_CODEC_ID_FRWU,
143  .priv_data_size = sizeof(FRWUContext),
144  .init = decode_init,
145  .close = decode_close,
146  .decode = decode_frame,
147  .capabilities = CODEC_CAP_DR1,
148  .long_name = NULL_IF_CONFIG_SMALL("Forward Uncompressed"),
149  .priv_class = &frwu_class,
150 };