00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "avcodec.h"
00024 #include "internal.h"
00025 #include "libavutil/intreadwrite.h"
00026
00027 static av_cold int avui_decode_init(AVCodecContext *avctx)
00028 {
00029 avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
00030
00031 avctx->coded_frame = avcodec_alloc_frame();
00032
00033 if (!avctx->coded_frame) {
00034 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
00035 return AVERROR(ENOMEM);
00036 }
00037
00038 return 0;
00039 }
00040
00041 static int avui_decode_frame(AVCodecContext *avctx, void *data,
00042 int *got_frame, AVPacket *avpkt)
00043 {
00044 AVFrame *pic = avctx->coded_frame;
00045 const uint8_t *src = avpkt->data, *extradata = avctx->extradata;
00046 const uint8_t *srca;
00047 uint8_t *y, *u, *v, *a;
00048 int transparent, interlaced = 1, skip, opaque_length, i, j, k;
00049 uint32_t extradata_size = avctx->extradata_size;
00050
00051 if (pic->data[0])
00052 avctx->release_buffer(avctx, pic);
00053
00054 while (extradata_size >= 24) {
00055 uint32_t atom_size = AV_RB32(extradata);
00056 if (!memcmp(&extradata[4], "APRGAPRG0001", 12)) {
00057 interlaced = extradata[19] != 1;
00058 break;
00059 }
00060 if (atom_size && atom_size <= extradata_size) {
00061 extradata += atom_size;
00062 extradata_size -= atom_size;
00063 } else {
00064 break;
00065 }
00066 }
00067 if (avctx->height == 486) {
00068 skip = 10;
00069 } else {
00070 skip = 16;
00071 }
00072 opaque_length = 2 * avctx->width * (avctx->height + skip) + 4 * interlaced;
00073 if (avpkt->size < opaque_length) {
00074 av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
00075 return AVERROR(EINVAL);
00076 }
00077 transparent = avctx->bits_per_coded_sample == 32 &&
00078 avpkt->size >= opaque_length * 2 + 4;
00079 srca = src + opaque_length + 5;
00080
00081 pic->reference = 0;
00082
00083 if (ff_get_buffer(avctx, pic) < 0) {
00084 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
00085 return AVERROR(ENOMEM);
00086 }
00087
00088 pic->key_frame = 1;
00089 pic->pict_type = AV_PICTURE_TYPE_I;
00090
00091 if (!interlaced) {
00092 src += avctx->width * skip;
00093 srca += avctx->width * skip;
00094 }
00095
00096 for (i = 0; i < interlaced + 1; i++) {
00097 src += avctx->width * skip;
00098 srca += avctx->width * skip;
00099 if (interlaced && avctx->height == 486) {
00100 y = pic->data[0] + (1 - i) * pic->linesize[0];
00101 u = pic->data[1] + (1 - i) * pic->linesize[1];
00102 v = pic->data[2] + (1 - i) * pic->linesize[2];
00103 a = pic->data[3] + (1 - i) * pic->linesize[3];
00104 } else {
00105 y = pic->data[0] + i * pic->linesize[0];
00106 u = pic->data[1] + i * pic->linesize[1];
00107 v = pic->data[2] + i * pic->linesize[2];
00108 a = pic->data[3] + i * pic->linesize[3];
00109 }
00110
00111 for (j = 0; j < avctx->height >> interlaced; j++) {
00112 for (k = 0; k < avctx->width >> 1; k++) {
00113 u[ k ] = *src++;
00114 y[2 * k ] = *src++;
00115 a[2 * k ] = 0xFF - (transparent ? *srca++ : 0);
00116 srca++;
00117 v[ k ] = *src++;
00118 y[2 * k + 1] = *src++;
00119 a[2 * k + 1] = 0xFF - (transparent ? *srca++ : 0);
00120 srca++;
00121 }
00122
00123 y += (interlaced + 1) * pic->linesize[0];
00124 u += (interlaced + 1) * pic->linesize[1];
00125 v += (interlaced + 1) * pic->linesize[2];
00126 a += (interlaced + 1) * pic->linesize[3];
00127 }
00128 src += 4;
00129 srca += 4;
00130 }
00131 *got_frame = 1;
00132 *(AVFrame *)data = *pic;
00133
00134 return avpkt->size;
00135 }
00136
00137 static av_cold int avui_decode_close(AVCodecContext *avctx)
00138 {
00139 if (avctx->coded_frame->data[0])
00140 avctx->release_buffer(avctx, avctx->coded_frame);
00141
00142 av_freep(&avctx->coded_frame);
00143
00144 return 0;
00145 }
00146
00147 AVCodec ff_avui_decoder = {
00148 .name = "avui",
00149 .type = AVMEDIA_TYPE_VIDEO,
00150 .id = AV_CODEC_ID_AVUI,
00151 .init = avui_decode_init,
00152 .decode = avui_decode_frame,
00153 .close = avui_decode_close,
00154 .capabilities = CODEC_CAP_DR1,
00155 .long_name = NULL_IF_CONFIG_SMALL("Avid Meridien Uncompressed"),
00156 };