libavcodec/mpegvideo_xvmc.c
302898fc
 /*
  * XVideo Motion Compensation
  * Copyright (c) 2003 Ivan Kalvachev
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
302898fc
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
302898fc
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
302898fc
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
302898fc
  */
 
2e7b4c84
 #include <limits.h>
2213f431
 #include <X11/extensions/XvMC.h>
2e7b4c84
 
 #include "avcodec.h"
 #include "mpegvideo.h"
 
 #undef NDEBUG
 #include <assert.h>
 
fd949a63
 #include "xvmc.h"
4440bd0d
 #include "xvmc_internal.h"
302898fc
 
8f38ff00
 /**
49bd8e4b
  * Initialize the block field of the MpegEncContext pointer passed as
8f38ff00
  * parameter after making sure that the data is not corrupted.
d758a5cb
  * In order to implement something like direct rendering instead of decoding
  * coefficients in s->blocks and then copying them, copy them directly
3d43e42f
  * into the data_blocks array provided by xvmc.
8f38ff00
  */
78f9a878
 void ff_xvmc_init_block(MpegEncContext *s)
148302e7
 {
1765aacb
     struct xvmc_pix_fmt *render = (struct xvmc_pix_fmt*)s->current_picture.f.data[2];
4e30fa6e
     assert(render && render->xvmc_id == AV_XVMC_ID);
d2d600b7
 
88bd7fdc
     s->block = (int16_t (*)[64])(render->data_blocks + render->next_free_data_block_num * 64);
a579db0c
 }
 
3d43e42f
 /**
49bd8e4b
  * Fill individual block pointers, so there are no gaps in the data_block array
63864cb8
  * in case not all blocks in the macroblock are coded.
3d43e42f
  */
78f9a878
 void ff_xvmc_pack_pblocks(MpegEncContext *s, int cbp)
148302e7
 {
b71bd0ed
     int i, j = 0;
148302e7
     const int mb_block_count = 4 + (1 << s->chroma_format);
a579db0c
 
148302e7
     cbp <<= 12-mb_block_count;
     for (i = 0; i < mb_block_count; i++) {
4ec58e13
         if (cbp & (1 << 11))
21effaa4
             s->pblocks[i] = &s->block[j++];
4ec58e13
         else
39dba5e8
             s->pblocks[i] = NULL;
42dfc2bf
         cbp += cbp;
a579db0c
     }
2e7b4c84
 }
 
9107f7cd
 /**
49bd8e4b
  * Find and store the surfaces that are used as reference frames.
9107f7cd
  * This function should be called for every new field and/or frame.
  * It should be safe to call the function a few times for the same field.
  */
6545996f
 int ff_xvmc_field_start(MpegEncContext *s, AVCodecContext *avctx)
148302e7
 {
1765aacb
     struct xvmc_pix_fmt *last, *next, *render = (struct xvmc_pix_fmt*)s->current_picture.f.data[2];
d2d600b7
     const int mb_block_count = 4 + (1 << s->chroma_format);
2e7b4c84
 
3ae01928
     assert(avctx);
4e30fa6e
     if (!render || render->xvmc_id != AV_XVMC_ID ||
859bb3cf
         !render->data_blocks || !render->mv_blocks ||
         (unsigned int)render->allocated_mv_blocks   > INT_MAX/(64*6) ||
         (unsigned int)render->allocated_data_blocks > INT_MAX/64     ||
         !render->p_surface) {
d2d600b7
         av_log(avctx, AV_LOG_ERROR,
                "Render token doesn't look as expected.\n");
a1db3b93
         return -1; // make sure that this is a render packet
d2d600b7
     }
2e7b4c84
 
cb18fb62
     if (render->filled_mv_blocks_num) {
9939841f
         av_log(avctx, AV_LOG_ERROR,
d2d600b7
                "Rendering surface contains %i unprocessed blocks.\n",
cb18fb62
                render->filled_mv_blocks_num);
013cebfb
         return -1;
9939841f
     }
cad1928f
     if (render->allocated_mv_blocks   < 1 ||
649c171a
         render->allocated_data_blocks <  render->allocated_mv_blocks*mb_block_count ||
         render->start_mv_blocks_num   >= render->allocated_mv_blocks                ||
         render->next_free_data_block_num >
                         render->allocated_data_blocks -
                         mb_block_count*(render->allocated_mv_blocks-render->start_mv_blocks_num)) {
d2d600b7
         av_log(avctx, AV_LOG_ERROR,
                "Rendering surface doesn't provide enough block structures to work with.\n");
         return -1;
     }
2e7b4c84
 
d76c5ed5
     render->picture_structure = s->picture_structure;
     render->flags             = s->first_field ? 0 : XVMC_SECOND_FIELD;
9e0a8a36
     render->p_future_surface  = NULL;
     render->p_past_surface    = NULL;
2e7b4c84
 
7e2e870e
     switch(s->pict_type) {
ce5e49b0
         case  AV_PICTURE_TYPE_I:
1f7c1d14
             return 0; // no prediction from other frames
ce5e49b0
         case  AV_PICTURE_TYPE_B:
1765aacb
             next = (struct xvmc_pix_fmt*)s->next_picture.f.data[2];
3ae01928
             if (!next)
148302e7
                 return -1;
4e30fa6e
             if (next->xvmc_id != AV_XVMC_ID)
148302e7
                 return -1;
2e7b4c84
             render->p_future_surface = next->p_surface;
1f7c1d14
             // no return here, going to set forward prediction
ce5e49b0
         case  AV_PICTURE_TYPE_P:
1765aacb
             last = (struct xvmc_pix_fmt*)s->last_picture.f.data[2];
63a21bc0
             if (!last)
1f7c1d14
                 last = render; // predict second field from the first
4e30fa6e
             if (last->xvmc_id != AV_XVMC_ID)
148302e7
                 return -1;
2e7b4c84
             render->p_past_surface = last->p_surface;
             return 0;
5e5c247a
     }
2e7b4c84
 
 return -1;
 }
 
0b2eb2e8
 /**
49bd8e4b
  * Complete frame/field rendering by passing any remaining blocks.
d758a5cb
  * Normally ff_draw_horiz_band() is called for each slice, however,
  * some leftover blocks, for example from error_resilience(), may remain.
0b2eb2e8
  * It should be safe to call the function a few times for the same field.
  */
78f9a878
 void ff_xvmc_field_end(MpegEncContext *s)
148302e7
 {
1765aacb
     struct xvmc_pix_fmt *render = (struct xvmc_pix_fmt*)s->current_picture.f.data[2];
3ae01928
     assert(render);
2e7b4c84
 
148302e7
     if (render->filled_mv_blocks_num > 0)
1d0feb5d
         ff_mpeg_draw_horiz_band(s, 0, 0);
2e7b4c84
 }
 
3d43e42f
 /**
49bd8e4b
  * Synthesize the data needed by XvMC to render one macroblock of data.
  * Fill all relevant fields, if necessary do IDCT.
3d43e42f
  */
78f9a878
 void ff_xvmc_decode_mb(MpegEncContext *s)
148302e7
 {
7e2e870e
     XvMCMacroBlock *mv_block;
f602b044
     struct xvmc_pix_fmt *render;
7e2e870e
     int i, cbp, blocks_per_mb;
2e7b4c84
 
39dba5e8
     const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
2e7b4c84
 
 
148302e7
     if (s->encoding) {
8ac5c1b2
         av_log(s->avctx, AV_LOG_ERROR, "XVMC doesn't support encoding!!!\n");
61b76987
         return;
2e7b4c84
     }
 
1f7c1d14
     // from MPV_decode_mb(), update DC predictors for P macroblocks
2e7b4c84
     if (!s->mb_intra) {
         s->last_dc[0] =
         s->last_dc[1] =
         s->last_dc[2] =  128 << s->intra_dc_precision;
     }
 
1f7c1d14
     // MC doesn't skip blocks
160d679c
     s->mb_skipped = 0;
2e7b4c84
 
 
d15876d3
     // Do I need to export quant when I could not perform postprocessing?
     // Anyway, it doesn't hurt.
759001c5
     s->current_picture.qscale_table[mb_xy] = s->qscale;
2e7b4c84
 
a1db3b93
     // start of XVMC-specific code
1765aacb
     render = (struct xvmc_pix_fmt*)s->current_picture.f.data[2];
3ae01928
     assert(render);
4e30fa6e
     assert(render->xvmc_id == AV_XVMC_ID);
2e7b4c84
     assert(render->mv_blocks);
302898fc
 
1f7c1d14
     // take the next free macroblock
115329f1
     mv_block = &render->mv_blocks[render->start_mv_blocks_num +
fe4be5db
                                   render->filled_mv_blocks_num];
2e7b4c84
 
4ef690f5
     mv_block->x        = s->mb_x;
     mv_block->y        = s->mb_y;
1f7c1d14
     mv_block->dct_type = s->interlaced_dct; // XVMC_DCT_TYPE_FRAME/FIELD;
4ec58e13
     if (s->mb_intra) {
1f7c1d14
         mv_block->macroblock_type = XVMC_MB_TYPE_INTRA; // no MC, all done
4ec58e13
     } else {
2e7b4c84
         mv_block->macroblock_type = XVMC_MB_TYPE_PATTERN;
 
148302e7
         if (s->mv_dir & MV_DIR_FORWARD) {
2d7d8cc7
             mv_block->macroblock_type |= XVMC_MB_TYPE_MOTION_FORWARD;
0439c09d
             // PMV[n][dir][xy] = mv[dir][n][xy]
2e7b4c84
             mv_block->PMV[0][0][0] = s->mv[0][0][0];
             mv_block->PMV[0][0][1] = s->mv[0][0][1];
             mv_block->PMV[1][0][0] = s->mv[0][1][0];
             mv_block->PMV[1][0][1] = s->mv[0][1][1];
         }
148302e7
         if (s->mv_dir & MV_DIR_BACKWARD) {
2d7d8cc7
             mv_block->macroblock_type |= XVMC_MB_TYPE_MOTION_BACKWARD;
2e7b4c84
             mv_block->PMV[0][1][0] = s->mv[1][0][0];
             mv_block->PMV[0][1][1] = s->mv[1][0][1];
             mv_block->PMV[1][1][0] = s->mv[1][1][0];
             mv_block->PMV[1][1][1] = s->mv[1][1][1];
         }
 
99df0aa5
         switch(s->mv_type) {
2e7b4c84
             case  MV_TYPE_16X16:
                 mv_block->motion_type = XVMC_PREDICTION_FRAME;
                 break;
             case  MV_TYPE_16X8:
                 mv_block->motion_type = XVMC_PREDICTION_16x8;
                 break;
             case  MV_TYPE_FIELD:
                 mv_block->motion_type = XVMC_PREDICTION_FIELD;
148302e7
                 if (s->picture_structure == PICT_FRAME) {
                     mv_block->PMV[0][0][1] <<= 1;
                     mv_block->PMV[1][0][1] <<= 1;
                     mv_block->PMV[0][1][1] <<= 1;
                     mv_block->PMV[1][1][1] <<= 1;
2e7b4c84
                 }
                 break;
             case  MV_TYPE_DMV:
                 mv_block->motion_type = XVMC_PREDICTION_DUAL_PRIME;
148302e7
                 if (s->picture_structure == PICT_FRAME) {
2e7b4c84
 
ba585726
                     mv_block->PMV[0][0][0] = s->mv[0][0][0];      // top from top
81189e4f
                     mv_block->PMV[0][0][1] = s->mv[0][0][1] << 1;
2e7b4c84
 
ba585726
                     mv_block->PMV[0][1][0] = s->mv[0][0][0];      // bottom from bottom
81189e4f
                     mv_block->PMV[0][1][1] = s->mv[0][0][1] << 1;
2e7b4c84
 
ba585726
                     mv_block->PMV[1][0][0] = s->mv[0][2][0];      // dmv00, top from bottom
81189e4f
                     mv_block->PMV[1][0][1] = s->mv[0][2][1] << 1; // dmv01
2e7b4c84
 
ba585726
                     mv_block->PMV[1][1][0] = s->mv[0][3][0];      // dmv10, bottom from top
81189e4f
                     mv_block->PMV[1][1][1] = s->mv[0][3][1] << 1; // dmv11
2e7b4c84
 
4ec58e13
                 } else {
ba585726
                     mv_block->PMV[0][1][0] = s->mv[0][2][0];      // dmv00
                     mv_block->PMV[0][1][1] = s->mv[0][2][1];      // dmv01
2e7b4c84
                 }
                 break;
             default:
                 assert(0);
         }
 
         mv_block->motion_vertical_field_select = 0;
 
1f7c1d14
         // set correct field references
148302e7
         if (s->mv_type == MV_TYPE_FIELD || s->mv_type == MV_TYPE_16X8) {
90509ec7
             mv_block->motion_vertical_field_select |= s->field_select[0][0];
7e2e870e
             mv_block->motion_vertical_field_select |= s->field_select[1][0] << 1;
             mv_block->motion_vertical_field_select |= s->field_select[0][1] << 2;
             mv_block->motion_vertical_field_select |= s->field_select[1][1] << 3;
2e7b4c84
         }
1f7c1d14
     } // !intra
a1db3b93
     // time to handle data blocks
2e7b4c84
     mv_block->index = render->next_free_data_block_num;
5e5c247a
 
2e7b4c84
     blocks_per_mb = 6;
148302e7
     if (s->chroma_format >= 2) {
4742762f
         blocks_per_mb = 4 + (1 << s->chroma_format);
2e7b4c84
     }
5e5c247a
 
1f7c1d14
     // calculate cbp
715731a3
     cbp = 0;
148302e7
     for (i = 0; i < blocks_per_mb; i++) {
2d7d8cc7
         cbp += cbp;
148302e7
         if (s->block_last_index[i] >= 0)
715731a3
             cbp++;
     }
115329f1
 
148302e7
     if (s->flags & CODEC_FLAG_GRAY) {
ba585726
         if (s->mb_intra) {                                   // intra frames are always full chroma blocks
148302e7
             for (i = 4; i < blocks_per_mb; i++) {
21effaa4
                 memset(s->pblocks[i], 0, sizeof(*s->pblocks[i]));  // so we need to clear them
148302e7
                 if (!render->unsigned_intra)
21effaa4
                     *s->pblocks[i][0] = 1 << 10;
a579db0c
             }
4ec58e13
         } else {
148302e7
             cbp &= 0xf << (blocks_per_mb - 4);
ba585726
             blocks_per_mb = 4;                               // luminance blocks only
715731a3
         }
a579db0c
     }
2e7b4c84
     mv_block->coded_block_pattern = cbp;
148302e7
     if (cbp == 0)
2e7b4c84
         mv_block->macroblock_type &= ~XVMC_MB_TYPE_PATTERN;
 
148302e7
     for (i = 0; i < blocks_per_mb; i++) {
         if (s->block_last_index[i] >= 0) {
7a74e067
             // I do not have unsigned_intra MOCO to test, hope it is OK.
fe655f67
             if (s->mb_intra && (render->idct || !render->unsigned_intra))
21effaa4
                 *s->pblocks[i][0] -= 1 << 10;
148302e7
             if (!render->idct) {
21effaa4
                 s->dsp.idct(*s->pblocks[i]);
a1db3b93
                 /* It is unclear if MC hardware requires pixel diff values to be
                  * in the range [-255;255]. TODO: Clipping if such hardware is
                  * ever found. As of now it would only be an unnecessary
                  * slowdown. */
2e7b4c84
             }
1f7c1d14
             // copy blocks only if the codec doesn't support pblocks reordering
148302e7
             if (s->avctx->xvmc_acceleration == 1) {
4742762f
                 memcpy(&render->data_blocks[render->next_free_data_block_num*64],
21effaa4
                        s->pblocks[i], sizeof(*s->pblocks[i]));
a579db0c
             }
             render->next_free_data_block_num++;
2e7b4c84
         }
     }
     render->filled_mv_blocks_num++;
 
bd0eddb7
     assert(render->filled_mv_blocks_num     <= render->allocated_mv_blocks);
     assert(render->next_free_data_block_num <= render->allocated_data_blocks);
     /* The above conditions should not be able to fail as long as this function
      * is used and the following 'if ()' automatically calls a callback to free
      * blocks. */
2e7b4c84
 
 
cad1928f
     if (render->filled_mv_blocks_num == render->allocated_mv_blocks)
1d0feb5d
         ff_mpeg_draw_horiz_band(s, 0, 0);
2e7b4c84
 }