static int decode_frame(AVCodecContext *avctx,

                        void *data,

                        int *data_size,

                        AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    const uint8_t *buf_end = avpkt->data + avpkt->size;

    int buf_size       = avpkt->size;

    DPXContext *const s = avctx->priv_data;

    AVFrame *picture  = data;

    AVFrame *const p = &s->picture;

    uint8_t *ptr;



    int magic_num, offset, endian;

    int x, y;

    int w, h, stride, bits_per_color, descriptor, elements, target_packet_size, source_packet_size;



    unsigned int rgbBuffer;



    magic_num = AV_RB32(buf);

    buf += 4;



    /* Check if the files "magic number" is "SDPX" which means it uses

     * big-endian or XPDS which is for little-endian files */

    if (magic_num == AV_RL32("SDPX")) {

        endian = 0;

    } else if (magic_num == AV_RB32("SDPX")) {

        endian = 1;

    } else {

        av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");





    offset = read32(&buf, endian);

    // Need to end in 0x304 offset from start of file

    buf = avpkt->data + 0x304;

    w = read32(&buf, endian);

    h = read32(&buf, endian);



    // Need to end in 0x320 to read the descriptor

    buf += 20;

    descriptor = buf[0];



    // Need to end in 0x323 to read the bits per color

    buf += 3;

    avctx->bits_per_raw_sample =

    bits_per_color = buf[0];



    switch (descriptor) {

        case 51: // RGBA

            elements = 4;

            break;

        case 50: // RGB

            elements = 3;

            break;

        default:

            av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);





    switch (bits_per_color) {

        case 8:

            if (elements == 4) {

                avctx->pix_fmt = PIX_FMT_RGBA;

            } else {

                avctx->pix_fmt = PIX_FMT_RGB24;


            source_packet_size = elements;

            target_packet_size = elements;

            break;

        case 10:

            avctx->pix_fmt = PIX_FMT_RGB48;

            target_packet_size = 6;

            source_packet_size = elements * 2;

            break;

        case 12:

        case 16:

            if (endian) {

                avctx->pix_fmt = PIX_FMT_RGB48BE;

            } else {

                avctx->pix_fmt = PIX_FMT_RGB48LE;


            target_packet_size = 6;

            source_packet_size = elements * 2;

            break;

        default:

            av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);





    if (s->picture.data[0])

        avctx->release_buffer(avctx, &s->picture);

    if (av_image_check_size(w, h, 0, avctx))


    if (w != avctx->width || h != avctx->height)

        avcodec_set_dimensions(avctx, w, h);

    if (avctx->get_buffer(avctx, p) < 0) {

        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");





    // Move pointer to offset from start of file

    buf =  avpkt->data + offset;



    ptr    = p->data[0];

    stride = p->linesize[0];



    switch (bits_per_color) {

        case 10:

            for (x = 0; x < avctx->height; x++) {

               uint16_t *dst = (uint16_t*)ptr;

               for (y = 0; y < avctx->width; y++) {

                   rgbBuffer = read32(&buf, endian);

                   // Read out the 10-bit colors and convert to 16-bit

                   *dst++ = make_16bit(rgbBuffer >> 16);

                   *dst++ = make_16bit(rgbBuffer >>  6);

                   *dst++ = make_16bit(rgbBuffer <<  4);


               ptr += stride;


            break;

        case 8:

        case 12: // Treat 12-bit as 16-bit

        case 16:





            if (source_packet_size == target_packet_size) {

                for (x = 0; x < avctx->height; x++) {

                    memcpy(ptr, buf, target_packet_size*avctx->width);

                    ptr += stride;

                    buf += source_packet_size*avctx->width;


            } else {

                for (x = 0; x < avctx->height; x++) {

                    uint8_t *dst = ptr;

                    for (y = 0; y < avctx->width; y++) {

                        memcpy(dst, buf, target_packet_size);

                        dst += target_packet_size;

                        buf += source_packet_size;


                    ptr += stride;



            break;




    *picture   = s->picture;

    *data_size = sizeof(AVPicture);



    return buf_size;

static int decode_frame(AVCodecContext *avctx,

                        void *data,

                        int *data_size,

                        AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    const uint8_t *buf_end = avpkt->data + avpkt->size;

    int buf_size       = avpkt->size;

    DPXContext *const s = avctx->priv_data;

    AVFrame *picture  = data;

    AVFrame *const p = &s->picture;

    uint8_t *ptr;



    int magic_num, offset, endian;

    int x, y;

    int w, h, stride, bits_per_color, descriptor, elements, target_packet_size, source_packet_size;



    unsigned int rgbBuffer;



    magic_num = AV_RB32(buf);

    buf += 4;



    



    if (magic_num == AV_RL32("SDPX")) {

        endian = 0;

    } else if (magic_num == AV_RB32("SDPX")) {

        endian = 1;

    } else {

        av_log(avctx, AV_LOG_ERROR, "DPX marker not found\n");





    offset = read32(&buf, endian);

    

    buf = avpkt->data + 0x304;

    w = read32(&buf, endian);

    h = read32(&buf, endian);



    

    buf += 20;

    descriptor = buf[0];



    

    buf += 3;

    avctx->bits_per_raw_sample =

    bits_per_color = buf[0];



    switch (descriptor) {

        case 51: 

            elements = 4;

            break;

        case 50: 

            elements = 3;

            break;

        default:

            av_log(avctx, AV_LOG_ERROR, "Unsupported descriptor %d\n", descriptor);





    switch (bits_per_color) {

        case 8:

            if (elements == 4) {

                avctx->pix_fmt = PIX_FMT_RGBA;

            } else {

                avctx->pix_fmt = PIX_FMT_RGB24;


            source_packet_size = elements;

            target_packet_size = elements;

            break;

        case 10:

            avctx->pix_fmt = PIX_FMT_RGB48;

            target_packet_size = 6;

            source_packet_size = elements * 2;

            break;

        case 12:

        case 16:

            if (endian) {

                avctx->pix_fmt = PIX_FMT_RGB48BE;

            } else {

                avctx->pix_fmt = PIX_FMT_RGB48LE;


            target_packet_size = 6;

            source_packet_size = elements * 2;

            break;

        default:

            av_log(avctx, AV_LOG_ERROR, "Unsupported color depth : %d\n", bits_per_color);





    if (s->picture.data[0])

        avctx->release_buffer(avctx, &s->picture);

    if (av_image_check_size(w, h, 0, avctx))


    if (w != avctx->width || h != avctx->height)

        avcodec_set_dimensions(avctx, w, h);

    if (avctx->get_buffer(avctx, p) < 0) {

        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");





    

    buf =  avpkt->data + offset;



    ptr    = p->data[0];

    stride = p->linesize[0];



    switch (bits_per_color) {

        case 10:

            for (x = 0; x < avctx->height; x++) {

               uint16_t *dst = (uint16_t*)ptr;

               for (y = 0; y < avctx->width; y++) {

                   rgbBuffer = read32(&buf, endian);

                   

                   *dst++ = make_16bit(rgbBuffer >> 16);

                   *dst++ = make_16bit(rgbBuffer >>  6);

                   *dst++ = make_16bit(rgbBuffer <<  4);


               ptr += stride;


            break;

        case 8:

        case 12: 

        case 16:





            if (source_packet_size == target_packet_size) {

                for (x = 0; x < avctx->height; x++) {

                    memcpy(ptr, buf, target_packet_size*avctx->width);

                    ptr += stride;

                    buf += source_packet_size*avctx->width;


            } else {

                for (x = 0; x < avctx->height; x++) {

                    uint8_t *dst = ptr;

                    for (y = 0; y < avctx->width; y++) {

                        memcpy(dst, buf, target_packet_size);

                        dst += target_packet_size;

                        buf += source_packet_size;


                    ptr += stride;



            break;




    *picture   = s->picture;

    *data_size = sizeof(AVPicture);



    return buf_size;

static int FUNC_0(AVCodecContext *VAR_0,

                        void *VAR_1,

                        int *VAR_2,

                        AVPacket *VAR_3)

{

    const uint8_t *VAR_4 = VAR_3->VAR_1;

    const uint8_t *VAR_5 = VAR_3->VAR_1 + VAR_3->size;

    int VAR_6       = VAR_3->size;

    DPXContext *const s = VAR_0->priv_data;

    AVFrame *picture  = VAR_1;

    AVFrame *const p = &s->picture;

    uint8_t *ptr;



    int VAR_7, VAR_8, VAR_9;

    int VAR_10, VAR_11;

    int VAR_12, VAR_13, VAR_14, VAR_15, VAR_16, VAR_17, VAR_18, VAR_19;



    unsigned int VAR_20;



    VAR_7 = AV_RB32(VAR_4);

    VAR_4 += 4;



    



    if (VAR_7 == AV_RL32("SDPX")) {

        VAR_9 = 0;

    } else if (VAR_7 == AV_RB32("SDPX")) {

        VAR_9 = 1;

    } else {

        av_log(VAR_0, AV_LOG_ERROR, "DPX marker not found\n");





    VAR_8 = read32(&VAR_4, VAR_9);

    

    VAR_4 = VAR_3->VAR_1 + 0x304;

    VAR_12 = read32(&VAR_4, VAR_9);

    VAR_13 = read32(&VAR_4, VAR_9);



    

    VAR_4 += 20;

    VAR_16 = VAR_4[0];



    

    VAR_4 += 3;

    VAR_0->bits_per_raw_sample =

    VAR_15 = VAR_4[0];



    switch (VAR_16) {

        case 51: 

            VAR_17 = 4;

            break;

        case 50: 

            VAR_17 = 3;

            break;

        default:

            av_log(VAR_0, AV_LOG_ERROR, "Unsupported VAR_16 %d\n", VAR_16);





    switch (VAR_15) {

        case 8:

            if (VAR_17 == 4) {

                VAR_0->pix_fmt = PIX_FMT_RGBA;

            } else {

                VAR_0->pix_fmt = PIX_FMT_RGB24;


            VAR_19 = VAR_17;

            VAR_18 = VAR_17;

            break;

        case 10:

            VAR_0->pix_fmt = PIX_FMT_RGB48;

            VAR_18 = 6;

            VAR_19 = VAR_17 * 2;

            break;

        case 12:

        case 16:

            if (VAR_9) {

                VAR_0->pix_fmt = PIX_FMT_RGB48BE;

            } else {

                VAR_0->pix_fmt = PIX_FMT_RGB48LE;


            VAR_18 = 6;

            VAR_19 = VAR_17 * 2;

            break;

        default:

            av_log(VAR_0, AV_LOG_ERROR, "Unsupported color depth : %d\n", VAR_15);





    if (s->picture.VAR_1[0])

        VAR_0->release_buffer(VAR_0, &s->picture);

    if (av_image_check_size(VAR_12, VAR_13, 0, VAR_0))


    if (VAR_12 != VAR_0->width || VAR_13 != VAR_0->height)

        avcodec_set_dimensions(VAR_0, VAR_12, VAR_13);

    if (VAR_0->get_buffer(VAR_0, p) < 0) {

        av_log(VAR_0, AV_LOG_ERROR, "get_buffer() failed\n");





    

    VAR_4 =  VAR_3->VAR_1 + VAR_8;



    ptr    = p->VAR_1[0];

    VAR_14 = p->linesize[0];



    switch (VAR_15) {

        case 10:

            for (VAR_10 = 0; VAR_10 < VAR_0->height; VAR_10++) {

               uint16_t *dst = (uint16_t*)ptr;

               for (VAR_11 = 0; VAR_11 < VAR_0->width; VAR_11++) {

                   VAR_20 = read32(&VAR_4, VAR_9);

                   

                   *dst++ = make_16bit(VAR_20 >> 16);

                   *dst++ = make_16bit(VAR_20 >>  6);

                   *dst++ = make_16bit(VAR_20 <<  4);


               ptr += VAR_14;


            break;

        case 8:

        case 12: 

        case 16:





            if (VAR_19 == VAR_18) {

                for (VAR_10 = 0; VAR_10 < VAR_0->height; VAR_10++) {

                    memcpy(ptr, VAR_4, VAR_18*VAR_0->width);

                    ptr += VAR_14;

                    VAR_4 += VAR_19*VAR_0->width;


            } else {

                for (VAR_10 = 0; VAR_10 < VAR_0->height; VAR_10++) {

                    uint8_t *dst = ptr;

                    for (VAR_11 = 0; VAR_11 < VAR_0->width; VAR_11++) {

                        memcpy(dst, VAR_4, VAR_18);

                        dst += VAR_18;

                        VAR_4 += VAR_19;


                    ptr += VAR_14;



            break;




    *picture   = s->picture;

    *VAR_2 = sizeof(AVPicture);



    return VAR_6;

static int FUNC_0(AVCodecContext *VAR_0,
void *VAR_1,
int *VAR_2,
AVPacket *VAR_3)
{ const uint8_t *VAR_4 = VAR_3->VAR_1; const uint8_t *VAR_5 = VAR_3->VAR_1 + VAR_3->size; int VAR_6       = VAR_3->size; DPXContext *const s = VAR_0->priv_data; AVFrame *picture  = VAR_1; AVFrame *const p = &s->picture; uint8_t *ptr; int VAR_7, VAR_8, VAR_9; int VAR_10, VAR_11; int VAR_12, VAR_13, VAR_14, VAR_15, VAR_16, VAR_17, VAR_18, VAR_19; unsigned int VAR_20; VAR_7 = AV_RB32(VAR_4); VAR_4 += 4; if (VAR_7 == AV_RL32("SDPX")) { VAR_9 = 0; } else if (VAR_7 == AV_RB32("SDPX")) { VAR_9 = 1; } else { av_log(VAR_0, AV_LOG_ERROR, "DPX marker not found\n"); VAR_8 = read32(&VAR_4, VAR_9); VAR_4 = VAR_3->VAR_1 + 0x304; VAR_12 = read32(&VAR_4, VAR_9); VAR_13 = read32(&VAR_4, VAR_9); VAR_4 += 20; VAR_16 = VAR_4[0]; VAR_4 += 3; VAR_0->bits_per_raw_sample =
VAR_15 = VAR_4[0]; switch (VAR_16) { case 51:
VAR_17 = 4; break; case 50:
VAR_17 = 3; break; default:
av_log(VAR_0, AV_LOG_ERROR, "Unsupported VAR_16 %d\n", VAR_16); switch (VAR_15) { case 8:
if (VAR_17 == 4) { VAR_0->pix_fmt = PIX_FMT_RGBA; } else { VAR_0->pix_fmt = PIX_FMT_RGB24; VAR_19 = VAR_17; VAR_18 = VAR_17; break; case 10:
VAR_0->pix_fmt = PIX_FMT_RGB48; VAR_18 = 6; VAR_19 = VAR_17 * 2; break; case 12:
case 16:
if (VAR_9) { VAR_0->pix_fmt = PIX_FMT_RGB48BE; } else { VAR_0->pix_fmt = PIX_FMT_RGB48LE; VAR_18 = 6; VAR_19 = VAR_17 * 2; break; default:
av_log(VAR_0, AV_LOG_ERROR, "Unsupported color depth : %d\n", VAR_15); if (s->picture.VAR_1[0])
VAR_0->release_buffer(VAR_0, &s->picture); if (av_image_check_size(VAR_12, VAR_13, 0, VAR_0))
if (VAR_12 != VAR_0->width || VAR_13 != VAR_0->height)
avcodec_set_dimensions(VAR_0, VAR_12, VAR_13); if (VAR_0->get_buffer(VAR_0, p) < 0) { av_log(VAR_0, AV_LOG_ERROR, "get_buffer() failed\n"); VAR_4 =  VAR_3->VAR_1 + VAR_8; ptr    = p->VAR_1[0]; VAR_14 = p->linesize[0]; switch (VAR_15) { case 10:
for (VAR_10 = 0; VAR_10 < VAR_0->height; VAR_10++) { uint16_t *dst = (uint16_t*)ptr; for (VAR_11 = 0; VAR_11 < VAR_0->width; VAR_11++) { VAR_20 = read32(&VAR_4, VAR_9); *dst++ = make_16bit(VAR_20 >> 16); *dst++ = make_16bit(VAR_20 >>  6); *dst++ = make_16bit(VAR_20 <<  4); ptr += VAR_14; break; case 8:
case 12:
case 16:
if (VAR_19 == VAR_18) { for (VAR_10 = 0; VAR_10 < VAR_0->height; VAR_10++) { memcpy(ptr, VAR_4, VAR_18*VAR_0->width); ptr += VAR_14; VAR_4 += VAR_19*VAR_0->width; } else { for (VAR_10 = 0; VAR_10 < VAR_0->height; VAR_10++) { uint8_t *dst = ptr; for (VAR_11 = 0; VAR_11 < VAR_0->width; VAR_11++) { memcpy(dst, VAR_4, VAR_18); dst += VAR_18; VAR_4 += VAR_19; ptr += VAR_14; break; *picture   = s->picture; *VAR_2 = sizeof(AVPicture); return VAR_6;