LCOV - code coverage report
Current view: top level - libavcodec - mpegpicture.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 197 244 80.7 %
Date: 2017-12-14 01:15:32 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       18574 : 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       18574 :     MAKE_WRITABLE(mb_var_buf);
      43       18574 :     MAKE_WRITABLE(mc_mb_var_buf);
      44       18574 :     MAKE_WRITABLE(mb_mean_buf);
      45       18574 :     MAKE_WRITABLE(mbskip_table_buf);
      46       18574 :     MAKE_WRITABLE(qscale_table_buf);
      47       18574 :     MAKE_WRITABLE(mb_type_buf);
      48             : 
      49       55722 :     for (i = 0; i < 2; i++) {
      50       37148 :         MAKE_WRITABLE(motion_val_buf[i]);
      51       37148 :         MAKE_WRITABLE(ref_index_buf[i]);
      52             :     }
      53             : 
      54       18574 :     return 0;
      55             : }
      56             : 
      57         409 : int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
      58             :                             ScratchpadContext *sc, int linesize)
      59             : {
      60         409 :     int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
      61             : 
      62         409 :     if (avctx->hwaccel)
      63           0 :         return 0;
      64             : 
      65         409 :     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         409 :     FF_ALLOCZ_ARRAY_OR_GOTO(avctx, sc->edge_emu_buffer, alloc_size, 4 * 70,
      77             :                       fail);
      78             : 
      79         409 :     FF_ALLOCZ_ARRAY_OR_GOTO(avctx, me->scratchpad, alloc_size, 4 * 16 * 2,
      80             :                       fail)
      81         409 :     me->temp            = me->scratchpad;
      82         409 :     sc->rd_scratchpad   = me->scratchpad;
      83         409 :     sc->b_scratchpad    = me->scratchpad;
      84         409 :     sc->obmc_scratchpad = me->scratchpad + 16;
      85             : 
      86         409 :     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       19191 : 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       19191 :     int edges_needed = av_codec_is_encoder(avctx->codec);
     101             :     int r, ret;
     102             : 
     103       19191 :     pic->tf.f = pic->f;
     104       38382 :     if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
     105       38382 :         avctx->codec_id != AV_CODEC_ID_VC1IMAGE  &&
     106       19191 :         avctx->codec_id != AV_CODEC_ID_MSS2) {
     107       19112 :         if (edges_needed) {
     108        8383 :             pic->f->width  = avctx->width  + 2 * EDGE_WIDTH;
     109        8383 :             pic->f->height = avctx->height + 2 * EDGE_WIDTH;
     110             :         }
     111             : 
     112       19112 :         r = ff_thread_get_buffer(avctx, &pic->tf,
     113       19112 :                                  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       19191 :     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       19191 :     if (edges_needed) {
     128             :         int i;
     129       33532 :         for (i = 0; pic->f->data[i]; i++) {
     130       75447 :             int offset = (EDGE_WIDTH >> (i ? chroma_y_shift : 0)) *
     131       25149 :                          pic->f->linesize[i] +
     132       25149 :                          (EDGE_WIDTH >> (i ? chroma_x_shift : 0));
     133       25149 :             pic->f->data[i] += offset;
     134             :         }
     135        8383 :         pic->f->width  = avctx->width;
     136        8383 :         pic->f->height = avctx->height;
     137             :     }
     138             : 
     139       19191 :     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       37985 :     if (linesize && (linesize   != pic->f->linesize[0] ||
     152       18794 :                      uvlinesize != pic->f->linesize[1])) {
     153           0 :         av_log(avctx, AV_LOG_ERROR,
     154             :                "get_buffer() failed (stride changed)\n");
     155           0 :         ff_mpeg_unref_picture(avctx, pic);
     156           0 :         return -1;
     157             :     }
     158             : 
     159       38382 :     if (av_pix_fmt_count_planes(pic->f->format) > 2 &&
     160       19191 :         pic->f->linesize[1] != pic->f->linesize[2]) {
     161           0 :         av_log(avctx, AV_LOG_ERROR,
     162             :                "get_buffer() failed (uv stride mismatch)\n");
     163           0 :         ff_mpeg_unref_picture(avctx, pic);
     164           0 :         return -1;
     165             :     }
     166             : 
     167       19588 :     if (!sc->edge_emu_buffer &&
     168         397 :         (ret = ff_mpeg_framesize_alloc(avctx, me, sc,
     169         397 :                                        pic->f->linesize[0])) < 0) {
     170           0 :         av_log(avctx, AV_LOG_ERROR,
     171             :                "get_buffer() failed to allocate context scratch buffers.\n");
     172           0 :         ff_mpeg_unref_picture(avctx, pic);
     173           0 :         return ret;
     174             :     }
     175             : 
     176       19191 :     return 0;
     177             : }
     178             : 
     179        1290 : static int alloc_picture_tables(AVCodecContext *avctx, Picture *pic, int encoding, int out_format,
     180             :                                 int mb_stride, int mb_width, int mb_height, int b8_stride)
     181             : {
     182        1290 :     const int big_mb_num    = mb_stride * (mb_height + 1) + 1;
     183        1290 :     const int mb_array_size = mb_stride * mb_height;
     184        1290 :     const int b8_array_size = b8_stride * mb_height * 2;
     185             :     int i;
     186             : 
     187             : 
     188        1290 :     pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
     189        1290 :     pic->qscale_table_buf = av_buffer_allocz(big_mb_num + mb_stride);
     190        1290 :     pic->mb_type_buf      = av_buffer_allocz((big_mb_num + mb_stride) *
     191             :                                              sizeof(uint32_t));
     192        1290 :     if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
     193           0 :         return AVERROR(ENOMEM);
     194             : 
     195        1290 :     if (encoding) {
     196         692 :         pic->mb_var_buf    = av_buffer_allocz(mb_array_size * sizeof(int16_t));
     197         692 :         pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
     198         692 :         pic->mb_mean_buf   = av_buffer_allocz(mb_array_size);
     199         692 :         if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
     200           0 :             return AVERROR(ENOMEM);
     201             :     }
     202             : 
     203        1476 :     if (out_format == FMT_H263 || encoding ||
     204             : #if FF_API_DEBUG_MV
     205             :         avctx->debug_mv ||
     206             : #endif
     207         186 :         (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS)) {
     208        1104 :         int mv_size        = 2 * (b8_array_size + 4) * sizeof(int16_t);
     209        1104 :         int ref_index_size = 4 * mb_array_size;
     210             : 
     211        3312 :         for (i = 0; mv_size && i < 2; i++) {
     212        2208 :             pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
     213        2208 :             pic->ref_index_buf[i]  = av_buffer_allocz(ref_index_size);
     214        2208 :             if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
     215           0 :                 return AVERROR(ENOMEM);
     216             :         }
     217             :     }
     218             : 
     219        1290 :     pic->alloc_mb_width  = mb_width;
     220        1290 :     pic->alloc_mb_height = mb_height;
     221             : 
     222        1290 :     return 0;
     223             : }
     224             : 
     225             : /**
     226             :  * Allocate a Picture.
     227             :  * The pixels are allocated/set by calling get_buffer() if shared = 0
     228             :  */
     229       19864 : int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me,
     230             :                      ScratchpadContext *sc, int shared, int encoding,
     231             :                      int chroma_x_shift, int chroma_y_shift, int out_format,
     232             :                      int mb_stride, int mb_width, int mb_height, int b8_stride,
     233             :                      ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
     234             : {
     235             :     int i, ret;
     236             : 
     237       19864 :     if (pic->qscale_table_buf)
     238       18574 :         if (   pic->alloc_mb_width  != mb_width
     239       18574 :             || pic->alloc_mb_height != mb_height)
     240           0 :             ff_free_picture_tables(pic);
     241             : 
     242       19864 :     if (shared) {
     243         673 :         av_assert0(pic->f->data[0]);
     244         673 :         pic->shared = 1;
     245             :     } else {
     246       19191 :         av_assert0(!pic->f->buf[0]);
     247       38382 :         if (alloc_frame_buffer(avctx, pic, me, sc,
     248             :                                chroma_x_shift, chroma_y_shift,
     249       38382 :                                *linesize, *uvlinesize) < 0)
     250           0 :             return -1;
     251             : 
     252       19191 :         *linesize   = pic->f->linesize[0];
     253       19191 :         *uvlinesize = pic->f->linesize[1];
     254             :     }
     255             : 
     256       19864 :     if (!pic->qscale_table_buf)
     257        1290 :         ret = alloc_picture_tables(avctx, pic, encoding, out_format,
     258             :                                    mb_stride, mb_width, mb_height, b8_stride);
     259             :     else
     260       18574 :         ret = make_tables_writable(pic);
     261       19864 :     if (ret < 0)
     262           0 :         goto fail;
     263             : 
     264       19864 :     if (encoding) {
     265        9056 :         pic->mb_var    = (uint16_t*)pic->mb_var_buf->data;
     266        9056 :         pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
     267        9056 :         pic->mb_mean   = pic->mb_mean_buf->data;
     268             :     }
     269             : 
     270       19864 :     pic->mbskip_table = pic->mbskip_table_buf->data;
     271       19864 :     pic->qscale_table = pic->qscale_table_buf->data + 2 * mb_stride + 1;
     272       19864 :     pic->mb_type      = (uint32_t*)pic->mb_type_buf->data + 2 * mb_stride + 1;
     273             : 
     274       19864 :     if (pic->motion_val_buf[0]) {
     275       48138 :         for (i = 0; i < 2; i++) {
     276       32092 :             pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
     277       32092 :             pic->ref_index[i]  = pic->ref_index_buf[i]->data;
     278             :         }
     279             :     }
     280             : 
     281       19864 :     return 0;
     282           0 : fail:
     283           0 :     av_log(avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
     284           0 :     ff_mpeg_unref_picture(avctx, pic);
     285           0 :     ff_free_picture_tables(pic);
     286           0 :     return AVERROR(ENOMEM);
     287             : }
     288             : 
     289             : /**
     290             :  * Deallocate a picture.
     291             :  */
     292      784866 : void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
     293             : {
     294      784866 :     int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
     295             : 
     296      784866 :     pic->tf.f = pic->f;
     297             :     /* WM Image / Screen codecs allocate internal buffers with different
     298             :      * dimensions / colorspaces; ignore user-defined callbacks for these. */
     299     1569732 :     if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
     300     1569732 :         avctx->codec_id != AV_CODEC_ID_VC1IMAGE  &&
     301      784866 :         avctx->codec_id != AV_CODEC_ID_MSS2)
     302      778304 :         ff_thread_release_buffer(avctx, &pic->tf);
     303        6562 :     else if (pic->f)
     304        6562 :         av_frame_unref(pic->f);
     305             : 
     306      784866 :     av_buffer_unref(&pic->hwaccel_priv_buf);
     307             : 
     308      784866 :     if (pic->needs_realloc)
     309        1348 :         ff_free_picture_tables(pic);
     310             : 
     311      784866 :     memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
     312      784866 : }
     313             : 
     314       73727 : int ff_update_picture_tables(Picture *dst, Picture *src)
     315             : {
     316             :      int i;
     317             : 
     318             : #define UPDATE_TABLE(table)                                                   \
     319             : do {                                                                          \
     320             :     if (src->table &&                                                         \
     321             :         (!dst->table || dst->table->buffer != src->table->buffer)) {          \
     322             :         av_buffer_unref(&dst->table);                                         \
     323             :         dst->table = av_buffer_ref(src->table);                               \
     324             :         if (!dst->table) {                                                    \
     325             :             ff_free_picture_tables(dst);                                      \
     326             :             return AVERROR(ENOMEM);                                           \
     327             :         }                                                                     \
     328             :     }                                                                         \
     329             : } while (0)
     330             : 
     331       73727 :     UPDATE_TABLE(mb_var_buf);
     332       73727 :     UPDATE_TABLE(mc_mb_var_buf);
     333       73727 :     UPDATE_TABLE(mb_mean_buf);
     334       73727 :     UPDATE_TABLE(mbskip_table_buf);
     335       73727 :     UPDATE_TABLE(qscale_table_buf);
     336       73727 :     UPDATE_TABLE(mb_type_buf);
     337      221181 :     for (i = 0; i < 2; i++) {
     338      147454 :         UPDATE_TABLE(motion_val_buf[i]);
     339      147454 :         UPDATE_TABLE(ref_index_buf[i]);
     340             :     }
     341             : 
     342       73727 :     dst->mb_var        = src->mb_var;
     343       73727 :     dst->mc_mb_var     = src->mc_mb_var;
     344       73727 :     dst->mb_mean       = src->mb_mean;
     345       73727 :     dst->mbskip_table  = src->mbskip_table;
     346       73727 :     dst->qscale_table  = src->qscale_table;
     347       73727 :     dst->mb_type       = src->mb_type;
     348      221181 :     for (i = 0; i < 2; i++) {
     349      147454 :         dst->motion_val[i] = src->motion_val[i];
     350      147454 :         dst->ref_index[i]  = src->ref_index[i];
     351             :     }
     352             : 
     353       73727 :     dst->alloc_mb_width  = src->alloc_mb_width;
     354       73727 :     dst->alloc_mb_height = src->alloc_mb_height;
     355             : 
     356       73727 :     return 0;
     357             : }
     358             : 
     359       73727 : int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
     360             : {
     361             :     int ret;
     362             : 
     363       73727 :     av_assert0(!dst->f->buf[0]);
     364       73727 :     av_assert0(src->f->buf[0]);
     365             : 
     366       73727 :     src->tf.f = src->f;
     367       73727 :     dst->tf.f = dst->f;
     368       73727 :     ret = ff_thread_ref_frame(&dst->tf, &src->tf);
     369       73727 :     if (ret < 0)
     370           0 :         goto fail;
     371             : 
     372       73727 :     ret = ff_update_picture_tables(dst, src);
     373       73727 :     if (ret < 0)
     374           0 :         goto fail;
     375             : 
     376       73727 :     if (src->hwaccel_picture_private) {
     377           0 :         dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
     378           0 :         if (!dst->hwaccel_priv_buf) {
     379           0 :             ret = AVERROR(ENOMEM);
     380           0 :             goto fail;
     381             :         }
     382           0 :         dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
     383             :     }
     384             : 
     385       73727 :     dst->field_picture           = src->field_picture;
     386       73727 :     dst->mb_var_sum              = src->mb_var_sum;
     387       73727 :     dst->mc_mb_var_sum           = src->mc_mb_var_sum;
     388       73727 :     dst->b_frame_score           = src->b_frame_score;
     389       73727 :     dst->needs_realloc           = src->needs_realloc;
     390       73727 :     dst->reference               = src->reference;
     391       73727 :     dst->shared                  = src->shared;
     392             : 
     393       73727 :     memcpy(dst->encoding_error, src->encoding_error,
     394             :            sizeof(dst->encoding_error));
     395             : 
     396       73727 :     return 0;
     397           0 : fail:
     398           0 :     ff_mpeg_unref_picture(avctx, dst);
     399           0 :     return ret;
     400             : }
     401             : 
     402       41139 : static inline int pic_is_unused(Picture *pic)
     403             : {
     404       41139 :     if (!pic->f->buf[0])
     405       18831 :         return 1;
     406       22308 :     if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
     407          20 :         return 1;
     408       22288 :     return 0;
     409             : }
     410             : 
     411       19524 : static int find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
     412             : {
     413             :     int i;
     414             : 
     415       19524 :     if (shared) {
     416         890 :         for (i = 0; i < MAX_PICTURE_COUNT; i++) {
     417         890 :             if (!picture[i].f->buf[0])
     418         673 :                 return i;
     419             :         }
     420             :     } else {
     421       41139 :         for (i = 0; i < MAX_PICTURE_COUNT; i++) {
     422       41139 :             if (pic_is_unused(&picture[i]))
     423       18851 :                 return i;
     424             :         }
     425             :     }
     426             : 
     427           0 :     av_log(avctx, AV_LOG_FATAL,
     428             :            "Internal error, picture buffer overflow\n");
     429             :     /* We could return -1, but the codec would crash trying to draw into a
     430             :      * non-existing frame anyway. This is safer than waiting for a random crash.
     431             :      * Also the return of this is never useful, an encoder must only allocate
     432             :      * as much as allowed in the specification. This has no relationship to how
     433             :      * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
     434             :      * enough for such valid streams).
     435             :      * Plus, a decoder has to check stream validity and remove frames if too
     436             :      * many reference frames are around. Waiting for "OOM" is not correct at
     437             :      * all. Similarly, missing reference frames have to be replaced by
     438             :      * interpolated/MC frames, anything else is a bug in the codec ...
     439             :      */
     440           0 :     abort();
     441             :     return -1;
     442             : }
     443             : 
     444       19524 : int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
     445             : {
     446       19524 :     int ret = find_unused_picture(avctx, picture, shared);
     447             : 
     448       19524 :     if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
     449       19524 :         if (picture[ret].needs_realloc) {
     450          20 :             picture[ret].needs_realloc = 0;
     451          20 :             ff_free_picture_tables(&picture[ret]);
     452          20 :             ff_mpeg_unref_picture(avctx, &picture[ret]);
     453             :         }
     454             :     }
     455       19524 :     return ret;
     456             : }
     457             : 
     458       31099 : void ff_free_picture_tables(Picture *pic)
     459             : {
     460             :     int i;
     461             : 
     462       31099 :     pic->alloc_mb_width  =
     463       31099 :     pic->alloc_mb_height = 0;
     464             : 
     465       31099 :     av_buffer_unref(&pic->mb_var_buf);
     466       31099 :     av_buffer_unref(&pic->mc_mb_var_buf);
     467       31099 :     av_buffer_unref(&pic->mb_mean_buf);
     468       31099 :     av_buffer_unref(&pic->mbskip_table_buf);
     469       31099 :     av_buffer_unref(&pic->qscale_table_buf);
     470       31099 :     av_buffer_unref(&pic->mb_type_buf);
     471             : 
     472       93297 :     for (i = 0; i < 2; i++) {
     473       62198 :         av_buffer_unref(&pic->motion_val_buf[i]);
     474       62198 :         av_buffer_unref(&pic->ref_index_buf[i]);
     475             :     }
     476       31099 : }

Generated by: LCOV version 1.13