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