LCOV - code coverage report
Current view: top level - libavcodec - mpegpicture.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 197 246 80.1 %
Date: 2018-05-20 11:54:08 Functions: 12 12 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Mpeg video formats-related picture management functions
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include <stdint.h>
      22             : 
      23             : #include "libavutil/avassert.h"
      24             : #include "libavutil/common.h"
      25             : #include "libavutil/pixdesc.h"
      26             : 
      27             : #include "avcodec.h"
      28             : #include "motion_est.h"
      29             : #include "mpegpicture.h"
      30             : #include "mpegutils.h"
      31             : 
      32       18507 : static int make_tables_writable(Picture *pic)
      33             : {
      34             :     int ret, i;
      35             : #define MAKE_WRITABLE(table) \
      36             : do {\
      37             :     if (pic->table &&\
      38             :        (ret = av_buffer_make_writable(&pic->table)) < 0)\
      39             :     return ret;\
      40             : } while (0)
      41             : 
      42       18507 :     MAKE_WRITABLE(mb_var_buf);
      43       18507 :     MAKE_WRITABLE(mc_mb_var_buf);
      44       18507 :     MAKE_WRITABLE(mb_mean_buf);
      45       18507 :     MAKE_WRITABLE(mbskip_table_buf);
      46       18507 :     MAKE_WRITABLE(qscale_table_buf);
      47       18507 :     MAKE_WRITABLE(mb_type_buf);
      48             : 
      49       55521 :     for (i = 0; i < 2; i++) {
      50       37014 :         MAKE_WRITABLE(motion_val_buf[i]);
      51       37014 :         MAKE_WRITABLE(ref_index_buf[i]);
      52             :     }
      53             : 
      54       18507 :     return 0;
      55             : }
      56             : 
      57         407 : int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
      58             :                             ScratchpadContext *sc, int linesize)
      59             : {
      60         407 :     int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
      61             : 
      62         407 :     if (avctx->hwaccel)
      63           0 :         return 0;
      64             : 
      65         407 :     if (linesize < 24) {
      66           0 :         av_log(avctx, AV_LOG_ERROR, "Image too small, temporary buffers cannot function\n");
      67           0 :         return AVERROR_PATCHWELCOME;
      68             :     }
      69             : 
      70             :     // edge emu needs blocksize + filter length - 1
      71             :     // (= 17x17 for  halfpel / 21x21 for H.264)
      72             :     // VC-1 computes luma and chroma simultaneously and needs 19X19 + 9x9
      73             :     // at uvlinesize. It supports only YUV420 so 24x24 is enough
      74             :     // linesize * interlaced * MBsize
      75             :     // we also use this buffer for encoding in encode_mb_internal() needig an additional 32 lines
      76         407 :     FF_ALLOCZ_ARRAY_OR_GOTO(avctx, sc->edge_emu_buffer, alloc_size, 4 * 70,
      77             :                       fail);
      78             : 
      79         407 :     FF_ALLOCZ_ARRAY_OR_GOTO(avctx, me->scratchpad, alloc_size, 4 * 16 * 2,
      80             :                       fail)
      81         407 :     me->temp            = me->scratchpad;
      82         407 :     sc->rd_scratchpad   = me->scratchpad;
      83         407 :     sc->b_scratchpad    = me->scratchpad;
      84         407 :     sc->obmc_scratchpad = me->scratchpad + 16;
      85             : 
      86         407 :     return 0;
      87           0 : fail:
      88           0 :     av_freep(&sc->edge_emu_buffer);
      89           0 :     return AVERROR(ENOMEM);
      90             : }
      91             : 
      92             : /**
      93             :  * Allocate a frame buffer
      94             :  */
      95       19141 : static int alloc_frame_buffer(AVCodecContext *avctx,  Picture *pic,
      96             :                               MotionEstContext *me, ScratchpadContext *sc,
      97             :                               int chroma_x_shift, int chroma_y_shift,
      98             :                               int linesize, int uvlinesize)
      99             : {
     100       19141 :     int edges_needed = av_codec_is_encoder(avctx->codec);
     101             :     int r, ret;
     102             : 
     103       19141 :     pic->tf.f = pic->f;
     104       38282 :     if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
     105       38282 :         avctx->codec_id != AV_CODEC_ID_VC1IMAGE  &&
     106       19141 :         avctx->codec_id != AV_CODEC_ID_MSS2) {
     107       19062 :         if (edges_needed) {
     108        8358 :             pic->f->width  = avctx->width  + 2 * EDGE_WIDTH;
     109        8358 :             pic->f->height = avctx->height + 2 * EDGE_WIDTH;
     110             :         }
     111             : 
     112       19062 :         r = ff_thread_get_buffer(avctx, &pic->tf,
     113       19062 :                                  pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
     114             :     } else {
     115          79 :         pic->f->width  = avctx->width;
     116          79 :         pic->f->height = avctx->height;
     117          79 :         pic->f->format = avctx->pix_fmt;
     118          79 :         r = avcodec_default_get_buffer2(avctx, pic->f, 0);
     119             :     }
     120             : 
     121       19141 :     if (r < 0 || !pic->f->buf[0]) {
     122           0 :         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
     123           0 :                r, pic->f->data[0]);
     124           0 :         return -1;
     125             :     }
     126             : 
     127       19141 :     if (edges_needed) {
     128             :         int i;
     129       33432 :         for (i = 0; pic->f->data[i]; i++) {
     130       75222 :             int offset = (EDGE_WIDTH >> (i ? chroma_y_shift : 0)) *
     131       25074 :                          pic->f->linesize[i] +
     132       25074 :                          (EDGE_WIDTH >> (i ? chroma_x_shift : 0));
     133       25074 :             pic->f->data[i] += offset;
     134             :         }
     135        8358 :         pic->f->width  = avctx->width;
     136        8358 :         pic->f->height = avctx->height;
     137             :     }
     138             : 
     139       19141 :     if (avctx->hwaccel) {
     140             :         assert(!pic->hwaccel_picture_private);
     141           0 :         if (avctx->hwaccel->frame_priv_data_size) {
     142           0 :             pic->hwaccel_priv_buf = av_buffer_allocz(avctx->hwaccel->frame_priv_data_size);
     143           0 :             if (!pic->hwaccel_priv_buf) {
     144           0 :                 av_log(avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
     145           0 :                 return -1;
     146             :             }
     147           0 :             pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
     148             :         }
     149             :     }
     150             : 
     151       19141 :     if ((linesize   &&   linesize != pic->f->linesize[0]) ||
     152       18746 :         (uvlinesize && uvlinesize != pic->f->linesize[1])) {
     153           0 :         av_log(avctx, AV_LOG_ERROR,
     154             :                "get_buffer() failed (stride changed: linesize=%d/%d uvlinesize=%d/%d)\n",
     155           0 :                linesize,   pic->f->linesize[0],
     156           0 :                uvlinesize, pic->f->linesize[1]);
     157           0 :         ff_mpeg_unref_picture(avctx, pic);
     158           0 :         return -1;
     159             :     }
     160             : 
     161       38282 :     if (av_pix_fmt_count_planes(pic->f->format) > 2 &&
     162       19141 :         pic->f->linesize[1] != pic->f->linesize[2]) {
     163           0 :         av_log(avctx, AV_LOG_ERROR,
     164             :                "get_buffer() failed (uv stride mismatch)\n");
     165           0 :         ff_mpeg_unref_picture(avctx, pic);
     166           0 :         return -1;
     167             :     }
     168             : 
     169       19536 :     if (!sc->edge_emu_buffer &&
     170         395 :         (ret = ff_mpeg_framesize_alloc(avctx, me, sc,
     171         395 :                                        pic->f->linesize[0])) < 0) {
     172           0 :         av_log(avctx, AV_LOG_ERROR,
     173             :                "get_buffer() failed to allocate context scratch buffers.\n");
     174           0 :         ff_mpeg_unref_picture(avctx, pic);
     175           0 :         return ret;
     176             :     }
     177             : 
     178       19141 :     return 0;
     179             : }
     180             : 
     181        1283 : static int alloc_picture_tables(AVCodecContext *avctx, Picture *pic, int encoding, int out_format,
     182             :                                 int mb_stride, int mb_width, int mb_height, int b8_stride)
     183             : {
     184        1283 :     const int big_mb_num    = mb_stride * (mb_height + 1) + 1;
     185        1283 :     const int mb_array_size = mb_stride * mb_height;
     186        1283 :     const int b8_array_size = b8_stride * mb_height * 2;
     187             :     int i;
     188             : 
     189             : 
     190        1283 :     pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
     191        1283 :     pic->qscale_table_buf = av_buffer_allocz(big_mb_num + mb_stride);
     192        1283 :     pic->mb_type_buf      = av_buffer_allocz((big_mb_num + mb_stride) *
     193             :                                              sizeof(uint32_t));
     194        1283 :     if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
     195           0 :         return AVERROR(ENOMEM);
     196             : 
     197        1283 :     if (encoding) {
     198         687 :         pic->mb_var_buf    = av_buffer_allocz(mb_array_size * sizeof(int16_t));
     199         687 :         pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
     200         687 :         pic->mb_mean_buf   = av_buffer_allocz(mb_array_size);
     201         687 :         if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
     202           0 :             return AVERROR(ENOMEM);
     203             :     }
     204             : 
     205        1467 :     if (out_format == FMT_H263 || encoding ||
     206             : #if FF_API_DEBUG_MV
     207             :         avctx->debug_mv ||
     208             : #endif
     209         184 :         (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS)) {
     210        1099 :         int mv_size        = 2 * (b8_array_size + 4) * sizeof(int16_t);
     211        1099 :         int ref_index_size = 4 * mb_array_size;
     212             : 
     213        3297 :         for (i = 0; mv_size && i < 2; i++) {
     214        2198 :             pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
     215        2198 :             pic->ref_index_buf[i]  = av_buffer_allocz(ref_index_size);
     216        2198 :             if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
     217           0 :                 return AVERROR(ENOMEM);
     218             :         }
     219             :     }
     220             : 
     221        1283 :     pic->alloc_mb_width  = mb_width;
     222        1283 :     pic->alloc_mb_height = mb_height;
     223             : 
     224        1283 :     return 0;
     225             : }
     226             : 
     227             : /**
     228             :  * Allocate a Picture.
     229             :  * The pixels are allocated/set by calling get_buffer() if shared = 0
     230             :  */
     231       19790 : int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me,
     232             :                      ScratchpadContext *sc, int shared, int encoding,
     233             :                      int chroma_x_shift, int chroma_y_shift, int out_format,
     234             :                      int mb_stride, int mb_width, int mb_height, int b8_stride,
     235             :                      ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
     236             : {
     237             :     int i, ret;
     238             : 
     239       19790 :     if (pic->qscale_table_buf)
     240       18507 :         if (   pic->alloc_mb_width  != mb_width
     241       18507 :             || pic->alloc_mb_height != mb_height)
     242           0 :             ff_free_picture_tables(pic);
     243             : 
     244       19790 :     if (shared) {
     245         649 :         av_assert0(pic->f->data[0]);
     246         649 :         pic->shared = 1;
     247             :     } else {
     248       19141 :         av_assert0(!pic->f->buf[0]);
     249       38282 :         if (alloc_frame_buffer(avctx, pic, me, sc,
     250             :                                chroma_x_shift, chroma_y_shift,
     251       38282 :                                *linesize, *uvlinesize) < 0)
     252           0 :             return -1;
     253             : 
     254       19141 :         *linesize   = pic->f->linesize[0];
     255       19141 :         *uvlinesize = pic->f->linesize[1];
     256             :     }
     257             : 
     258       19790 :     if (!pic->qscale_table_buf)
     259        1283 :         ret = alloc_picture_tables(avctx, pic, encoding, out_format,
     260             :                                    mb_stride, mb_width, mb_height, b8_stride);
     261             :     else
     262       18507 :         ret = make_tables_writable(pic);
     263       19790 :     if (ret < 0)
     264           0 :         goto fail;
     265             : 
     266       19790 :     if (encoding) {
     267        9007 :         pic->mb_var    = (uint16_t*)pic->mb_var_buf->data;
     268        9007 :         pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
     269        9007 :         pic->mb_mean   = pic->mb_mean_buf->data;
     270             :     }
     271             : 
     272       19790 :     pic->mbskip_table = pic->mbskip_table_buf->data;
     273       19790 :     pic->qscale_table = pic->qscale_table_buf->data + 2 * mb_stride + 1;
     274       19790 :     pic->mb_type      = (uint32_t*)pic->mb_type_buf->data + 2 * mb_stride + 1;
     275             : 
     276       19790 :     if (pic->motion_val_buf[0]) {
     277       47991 :         for (i = 0; i < 2; i++) {
     278       31994 :             pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
     279       31994 :             pic->ref_index[i]  = pic->ref_index_buf[i]->data;
     280             :         }
     281             :     }
     282             : 
     283       19790 :     return 0;
     284           0 : fail:
     285           0 :     av_log(avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
     286           0 :     ff_mpeg_unref_picture(avctx, pic);
     287           0 :     ff_free_picture_tables(pic);
     288           0 :     return AVERROR(ENOMEM);
     289             : }
     290             : 
     291             : /**
     292             :  * Deallocate a picture.
     293             :  */
     294      782791 : void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
     295             : {
     296      782791 :     int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
     297             : 
     298      782791 :     pic->tf.f = pic->f;
     299             :     /* WM Image / Screen codecs allocate internal buffers with different
     300             :      * dimensions / colorspaces; ignore user-defined callbacks for these. */
     301     1565582 :     if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
     302     1565582 :         avctx->codec_id != AV_CODEC_ID_VC1IMAGE  &&
     303      782791 :         avctx->codec_id != AV_CODEC_ID_MSS2)
     304      776229 :         ff_thread_release_buffer(avctx, &pic->tf);
     305        6562 :     else if (pic->f)
     306        6562 :         av_frame_unref(pic->f);
     307             : 
     308      782791 :     av_buffer_unref(&pic->hwaccel_priv_buf);
     309             : 
     310      782791 :     if (pic->needs_realloc)
     311        1348 :         ff_free_picture_tables(pic);
     312             : 
     313      782791 :     memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
     314      782791 : }
     315             : 
     316       73529 : int ff_update_picture_tables(Picture *dst, Picture *src)
     317             : {
     318             :      int i;
     319             : 
     320             : #define UPDATE_TABLE(table)                                                   \
     321             : do {                                                                          \
     322             :     if (src->table &&                                                         \
     323             :         (!dst->table || dst->table->buffer != src->table->buffer)) {          \
     324             :         av_buffer_unref(&dst->table);                                         \
     325             :         dst->table = av_buffer_ref(src->table);                               \
     326             :         if (!dst->table) {                                                    \
     327             :             ff_free_picture_tables(dst);                                      \
     328             :             return AVERROR(ENOMEM);                                           \
     329             :         }                                                                     \
     330             :     }                                                                         \
     331             : } while (0)
     332             : 
     333       73529 :     UPDATE_TABLE(mb_var_buf);
     334       73529 :     UPDATE_TABLE(mc_mb_var_buf);
     335       73529 :     UPDATE_TABLE(mb_mean_buf);
     336       73529 :     UPDATE_TABLE(mbskip_table_buf);
     337       73529 :     UPDATE_TABLE(qscale_table_buf);
     338       73529 :     UPDATE_TABLE(mb_type_buf);
     339      220587 :     for (i = 0; i < 2; i++) {
     340      147058 :         UPDATE_TABLE(motion_val_buf[i]);
     341      147058 :         UPDATE_TABLE(ref_index_buf[i]);
     342             :     }
     343             : 
     344       73529 :     dst->mb_var        = src->mb_var;
     345       73529 :     dst->mc_mb_var     = src->mc_mb_var;
     346       73529 :     dst->mb_mean       = src->mb_mean;
     347       73529 :     dst->mbskip_table  = src->mbskip_table;
     348       73529 :     dst->qscale_table  = src->qscale_table;
     349       73529 :     dst->mb_type       = src->mb_type;
     350      220587 :     for (i = 0; i < 2; i++) {
     351      147058 :         dst->motion_val[i] = src->motion_val[i];
     352      147058 :         dst->ref_index[i]  = src->ref_index[i];
     353             :     }
     354             : 
     355       73529 :     dst->alloc_mb_width  = src->alloc_mb_width;
     356       73529 :     dst->alloc_mb_height = src->alloc_mb_height;
     357             : 
     358       73529 :     return 0;
     359             : }
     360             : 
     361       73529 : int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
     362             : {
     363             :     int ret;
     364             : 
     365       73529 :     av_assert0(!dst->f->buf[0]);
     366       73529 :     av_assert0(src->f->buf[0]);
     367             : 
     368       73529 :     src->tf.f = src->f;
     369       73529 :     dst->tf.f = dst->f;
     370       73529 :     ret = ff_thread_ref_frame(&dst->tf, &src->tf);
     371       73529 :     if (ret < 0)
     372           0 :         goto fail;
     373             : 
     374       73529 :     ret = ff_update_picture_tables(dst, src);
     375       73529 :     if (ret < 0)
     376           0 :         goto fail;
     377             : 
     378       73529 :     if (src->hwaccel_picture_private) {
     379           0 :         dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
     380           0 :         if (!dst->hwaccel_priv_buf) {
     381           0 :             ret = AVERROR(ENOMEM);
     382           0 :             goto fail;
     383             :         }
     384           0 :         dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
     385             :     }
     386             : 
     387       73529 :     dst->field_picture           = src->field_picture;
     388       73529 :     dst->mb_var_sum              = src->mb_var_sum;
     389       73529 :     dst->mc_mb_var_sum           = src->mc_mb_var_sum;
     390       73529 :     dst->b_frame_score           = src->b_frame_score;
     391       73529 :     dst->needs_realloc           = src->needs_realloc;
     392       73529 :     dst->reference               = src->reference;
     393       73529 :     dst->shared                  = src->shared;
     394             : 
     395       73529 :     memcpy(dst->encoding_error, src->encoding_error,
     396             :            sizeof(dst->encoding_error));
     397             : 
     398       73529 :     return 0;
     399           0 : fail:
     400           0 :     ff_mpeg_unref_picture(avctx, dst);
     401           0 :     return ret;
     402             : }
     403             : 
     404       41006 : static inline int pic_is_unused(Picture *pic)
     405             : {
     406       41006 :     if (!pic->f->buf[0])
     407       18781 :         return 1;
     408       22225 :     if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
     409          20 :         return 1;
     410       22205 :     return 0;
     411             : }
     412             : 
     413       19450 : static int find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
     414             : {
     415             :     int i;
     416             : 
     417       19450 :     if (shared) {
     418         852 :         for (i = 0; i < MAX_PICTURE_COUNT; i++) {
     419         852 :             if (!picture[i].f->buf[0])
     420         649 :                 return i;
     421             :         }
     422             :     } else {
     423       41006 :         for (i = 0; i < MAX_PICTURE_COUNT; i++) {
     424       41006 :             if (pic_is_unused(&picture[i]))
     425       18801 :                 return i;
     426             :         }
     427             :     }
     428             : 
     429           0 :     av_log(avctx, AV_LOG_FATAL,
     430             :            "Internal error, picture buffer overflow\n");
     431             :     /* We could return -1, but the codec would crash trying to draw into a
     432             :      * non-existing frame anyway. This is safer than waiting for a random crash.
     433             :      * Also the return of this is never useful, an encoder must only allocate
     434             :      * as much as allowed in the specification. This has no relationship to how
     435             :      * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
     436             :      * enough for such valid streams).
     437             :      * Plus, a decoder has to check stream validity and remove frames if too
     438             :      * many reference frames are around. Waiting for "OOM" is not correct at
     439             :      * all. Similarly, missing reference frames have to be replaced by
     440             :      * interpolated/MC frames, anything else is a bug in the codec ...
     441             :      */
     442           0 :     abort();
     443             :     return -1;
     444             : }
     445             : 
     446       19450 : int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
     447             : {
     448       19450 :     int ret = find_unused_picture(avctx, picture, shared);
     449             : 
     450       19450 :     if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
     451       19450 :         if (picture[ret].needs_realloc) {
     452          20 :             picture[ret].needs_realloc = 0;
     453          20 :             ff_free_picture_tables(&picture[ret]);
     454          20 :             ff_mpeg_unref_picture(avctx, &picture[ret]);
     455             :         }
     456             :     }
     457       19450 :     return ret;
     458             : }
     459             : 
     460       31018 : void ff_free_picture_tables(Picture *pic)
     461             : {
     462             :     int i;
     463             : 
     464       31018 :     pic->alloc_mb_width  =
     465       31018 :     pic->alloc_mb_height = 0;
     466             : 
     467       31018 :     av_buffer_unref(&pic->mb_var_buf);
     468       31018 :     av_buffer_unref(&pic->mc_mb_var_buf);
     469       31018 :     av_buffer_unref(&pic->mb_mean_buf);
     470       31018 :     av_buffer_unref(&pic->mbskip_table_buf);
     471       31018 :     av_buffer_unref(&pic->qscale_table_buf);
     472       31018 :     av_buffer_unref(&pic->mb_type_buf);
     473             : 
     474       93054 :     for (i = 0; i < 2; i++) {
     475       62036 :         av_buffer_unref(&pic->motion_val_buf[i]);
     476       62036 :         av_buffer_unref(&pic->ref_index_buf[i]);
     477             :     }
     478       31018 : }

Generated by: LCOV version 1.13