• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/dpxenc.c

Go to the documentation of this file.
00001 /*
00002  * DPX (.dpx) image encoder
00003  * Copyright (c) 2011 Peter Ross <pross@xvid.org>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "libavutil/intreadwrite.h"
00023 #include "libavutil/imgutils.h"
00024 #include "avcodec.h"
00025 
00026 typedef struct DPXContext {
00027     AVFrame picture;
00028     int big_endian;
00029     int bits_per_component;
00030     int descriptor;
00031 } DPXContext;
00032 
00033 static av_cold int encode_init(AVCodecContext *avctx)
00034 {
00035     DPXContext *s = avctx->priv_data;
00036 
00037     avctx->coded_frame = &s->picture;
00038     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00039     avctx->coded_frame->key_frame = 1;
00040 
00041     s->big_endian         = 1;
00042     s->bits_per_component = 8;
00043     s->descriptor         = 50; /* RGB */
00044 
00045     switch (avctx->pix_fmt) {
00046     case PIX_FMT_RGB24:
00047         break;
00048     case PIX_FMT_RGBA:
00049         s->descriptor = 51; /* RGBA */
00050         break;
00051     case PIX_FMT_RGB48LE:
00052         s->big_endian = 0;
00053     case PIX_FMT_RGB48BE:
00054         s->bits_per_component = avctx->bits_per_raw_sample ? avctx->bits_per_raw_sample : 16;
00055         break;
00056     default:
00057         av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
00058         return -1;
00059     }
00060 
00061     return 0;
00062 }
00063 
00064 #define write16(p, value) \
00065 do { \
00066     if (s->big_endian) AV_WB16(p, value); \
00067     else               AV_WL16(p, value); \
00068 } while(0)
00069 
00070 #define write32(p, value) \
00071 do { \
00072     if (s->big_endian) AV_WB32(p, value); \
00073     else               AV_WL32(p, value); \
00074 } while(0)
00075 
00076 static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic, uint8_t *dst)
00077 {
00078     DPXContext *s = avctx->priv_data;
00079     const uint8_t *src = pic->data[0];
00080     int x, y;
00081 
00082     for (y = 0; y < avctx->height; y++) {
00083         for (x = 0; x < avctx->width; x++) {
00084             int value;
00085             if ((avctx->pix_fmt & 1)) {
00086                 value = ((AV_RB16(src + 6*x + 4) & 0xFFC0) >> 4)
00087                       | ((AV_RB16(src + 6*x + 2) & 0xFFC0) << 6)
00088                       | ((AV_RB16(src + 6*x + 0) & 0xFFC0) << 16);
00089             } else {
00090                 value = ((AV_RL16(src + 6*x + 4) & 0xFFC0) >> 4)
00091                       | ((AV_RL16(src + 6*x + 2) & 0xFFC0) << 6)
00092                       | ((AV_RL16(src + 6*x + 0) & 0xFFC0) << 16);
00093             }
00094             write32(dst, value);
00095             dst += 4;
00096         }
00097         src += pic->linesize[0];
00098     }
00099 }
00100 
00101 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
00102 {
00103     DPXContext *s = avctx->priv_data;
00104     int size;
00105 
00106 #define HEADER_SIZE 1664  /* DPX Generic header */
00107     if (buf_size < HEADER_SIZE)
00108         return -1;
00109 
00110     memset(buf, 0, HEADER_SIZE);
00111 
00112     /* File information header */
00113     write32(buf,       MKBETAG('S','D','P','X'));
00114     write32(buf +   4, HEADER_SIZE);
00115     memcpy (buf +   8, "V1.0", 4);
00116     write32(buf +  20, 1); /* new image */
00117     write32(buf +  24, HEADER_SIZE);
00118     memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
00119     write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
00120 
00121     /* Image information header */
00122     write16(buf + 768, 0); /* orientation; left to right, top to bottom */
00123     write16(buf + 770, 1); /* number of elements */
00124     write32(buf + 772, avctx->width);
00125     write32(buf + 776, avctx->height);
00126     buf[800] = s->descriptor;
00127     buf[801] = 2; /* linear transfer */
00128     buf[802] = 2; /* linear colorimetric */
00129     buf[803] = s->bits_per_component;
00130     write16(buf + 804, s->bits_per_component == 10 ? 1 : 0); /* packing method */
00131 
00132     /* Image source information header */
00133     write32(buf + 1628, avctx->sample_aspect_ratio.num);
00134     write32(buf + 1632, avctx->sample_aspect_ratio.den);
00135 
00136     switch(s->bits_per_component) {
00137     case 8:
00138     case 16:
00139         size = avpicture_layout(data, avctx->pix_fmt,
00140                                 avctx->width, avctx->height,
00141                                 buf + HEADER_SIZE, buf_size - HEADER_SIZE);
00142         if (size < 0)
00143             return size;
00144         break;
00145     case 10:
00146         size = avctx->height * avctx->width * 4;
00147         if (buf_size < HEADER_SIZE + size)
00148             return -1;
00149         encode_rgb48_10bit(avctx, data, buf + HEADER_SIZE);
00150         break;
00151     default:
00152         av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
00153         return -1;
00154     }
00155 
00156     size += HEADER_SIZE;
00157 
00158     write32(buf + 16, size); /* file size */
00159     return size;
00160 }
00161 
00162 AVCodec ff_dpx_encoder = {
00163     .name = "dpx",
00164     .type = AVMEDIA_TYPE_VIDEO,
00165     .id   = CODEC_ID_DPX,
00166     .priv_data_size = sizeof(DPXContext),
00167     .init   = encode_init,
00168     .encode = encode_frame,
00169     .pix_fmts = (const enum PixelFormat[]){
00170         PIX_FMT_RGB24,
00171         PIX_FMT_RGBA,
00172         PIX_FMT_RGB48LE,
00173         PIX_FMT_RGB48BE,
00174         PIX_FMT_NONE},
00175     .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
00176 };

Generated on Fri Feb 22 2013 07:24:26 for FFmpeg by  doxygen 1.7.1