Libav
mpegpicture.c
Go to the documentation of this file.
1 /*
2  * Mpeg video formats-related picture management functions
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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 
26 #include "avcodec.h"
27 #include "motion_est.h"
28 #include "mpegpicture.h"
29 #include "mpegutils.h"
30 
31 static int make_tables_writable(Picture *pic)
32 {
33  int ret, i;
34 #define MAKE_WRITABLE(table) \
35 do {\
36  if (pic->table &&\
37  (ret = av_buffer_make_writable(&pic->table)) < 0)\
38  return ret;\
39 } while (0)
40 
41  MAKE_WRITABLE(mb_var_buf);
42  MAKE_WRITABLE(mc_mb_var_buf);
43  MAKE_WRITABLE(mb_mean_buf);
44  MAKE_WRITABLE(mbskip_table_buf);
45  MAKE_WRITABLE(qscale_table_buf);
46  MAKE_WRITABLE(mb_type_buf);
47 
48  for (i = 0; i < 2; i++) {
49  MAKE_WRITABLE(motion_val_buf[i]);
50  MAKE_WRITABLE(ref_index_buf[i]);
51  }
52 
53  return 0;
54 }
55 
57  ScratchpadContext *sc, int linesize)
58 {
59  int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
60 
61  // edge emu needs blocksize + filter length - 1
62  // (= 17x17 for halfpel / 21x21 for H.264)
63  // VC-1 computes luma and chroma simultaneously and needs 19X19 + 9x9
64  // at uvlinesize. It supports only YUV420 so 24x24 is enough
65  // linesize * interlaced * MBsize
66  FF_ALLOCZ_OR_GOTO(avctx, sc->edge_emu_buffer, alloc_size * 2 * 24,
67  fail);
68 
69  FF_ALLOCZ_OR_GOTO(avctx, me->scratchpad, alloc_size * 2 * 16 * 3,
70  fail)
71  me->temp = me->scratchpad;
72  sc->rd_scratchpad = me->scratchpad;
73  sc->b_scratchpad = me->scratchpad;
74  sc->obmc_scratchpad = me->scratchpad + 16;
75 
76  return 0;
77 fail:
79  return AVERROR(ENOMEM);
80 }
81 
85 static int alloc_frame_buffer(AVCodecContext *avctx, Picture *pic,
87  int chroma_x_shift, int chroma_y_shift,
88  int linesize, int uvlinesize)
89 {
90  int edges_needed = av_codec_is_encoder(avctx->codec);
91  int r, ret;
92 
93  pic->tf.f = pic->f;
94  if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
95  avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
96  avctx->codec_id != AV_CODEC_ID_MSS2) {
97  if (edges_needed) {
98  pic->f->width = avctx->width + 2 * EDGE_WIDTH;
99  pic->f->height = avctx->height + 2 * EDGE_WIDTH;
100  }
101 
102  r = ff_thread_get_buffer(avctx, &pic->tf,
103  pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
104  } else {
105  pic->f->width = avctx->width;
106  pic->f->height = avctx->height;
107  pic->f->format = avctx->pix_fmt;
108  r = avcodec_default_get_buffer2(avctx, pic->f, 0);
109  }
110 
111  if (r < 0 || !pic->f->buf[0]) {
112  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
113  r, pic->f->data[0]);
114  return -1;
115  }
116 
117  if (edges_needed) {
118  int i;
119  for (i = 0; pic->f->data[i]; i++) {
120  int offset = (EDGE_WIDTH >> (i ? chroma_y_shift : 0)) *
121  pic->f->linesize[i] +
122  (EDGE_WIDTH >> (i ? chroma_x_shift : 0));
123  pic->f->data[i] += offset;
124  }
125  pic->f->width = avctx->width;
126  pic->f->height = avctx->height;
127  }
128 
129  if (avctx->hwaccel) {
130  assert(!pic->hwaccel_picture_private);
131  if (avctx->hwaccel->frame_priv_data_size) {
133  if (!pic->hwaccel_priv_buf) {
134  av_log(avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
135  return -1;
136  }
138  }
139  }
140 
141  if (linesize && (linesize != pic->f->linesize[0] ||
142  uvlinesize != pic->f->linesize[1])) {
143  av_log(avctx, AV_LOG_ERROR,
144  "get_buffer() failed (stride changed)\n");
145  ff_mpeg_unref_picture(avctx, pic);
146  return -1;
147  }
148 
149  if (pic->f->linesize[1] != pic->f->linesize[2]) {
150  av_log(avctx, AV_LOG_ERROR,
151  "get_buffer() failed (uv stride mismatch)\n");
152  ff_mpeg_unref_picture(avctx, pic);
153  return -1;
154  }
155 
156  if (!sc->edge_emu_buffer &&
157  (ret = ff_mpeg_framesize_alloc(avctx, me, sc,
158  pic->f->linesize[0])) < 0) {
159  av_log(avctx, AV_LOG_ERROR,
160  "get_buffer() failed to allocate context scratch buffers.\n");
161  ff_mpeg_unref_picture(avctx, pic);
162  return ret;
163  }
164 
165  return 0;
166 }
167 
168 static int alloc_picture_tables(Picture *pic, int encoding, int out_format,
169  int mb_stride, int mb_height, int b8_stride)
170 {
171  const int big_mb_num = mb_stride * (mb_height + 1) + 1;
172  const int mb_array_size = mb_stride * mb_height;
173  const int b8_array_size = b8_stride * mb_height * 2;
174  int i;
175 
176 
177  pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
178  pic->qscale_table_buf = av_buffer_allocz(big_mb_num + mb_stride);
179  pic->mb_type_buf = av_buffer_allocz((big_mb_num + mb_stride) *
180  sizeof(uint32_t));
181  if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
182  return AVERROR(ENOMEM);
183 
184  if (encoding) {
185  pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
186  pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
187  pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
188  if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
189  return AVERROR(ENOMEM);
190  }
191 
192  if (out_format == FMT_H263 || encoding) {
193  int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
194  int ref_index_size = 4 * mb_array_size;
195 
196  for (i = 0; mv_size && i < 2; i++) {
197  pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
198  pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
199  if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
200  return AVERROR(ENOMEM);
201  }
202  }
203 
204  return 0;
205 }
206 
212  ScratchpadContext *sc, int shared, int encoding,
213  int chroma_x_shift, int chroma_y_shift, int out_format,
214  int mb_stride, int mb_height, int b8_stride,
215  ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
216 {
217  int i, ret;
218 
219  if (shared) {
220  assert(pic->f->data[0]);
221  pic->shared = 1;
222  } else {
223  assert(!pic->f->buf[0]);
224  if (alloc_frame_buffer(avctx, pic, me, sc,
225  chroma_x_shift, chroma_y_shift,
226  *linesize, *uvlinesize) < 0)
227  return -1;
228 
229  *linesize = pic->f->linesize[0];
230  *uvlinesize = pic->f->linesize[1];
231  }
232 
233  if (!pic->qscale_table_buf)
234  ret = alloc_picture_tables(pic, encoding, out_format,
235  mb_stride, mb_height, b8_stride);
236  else
237  ret = make_tables_writable(pic);
238  if (ret < 0)
239  goto fail;
240 
241  if (encoding) {
242  pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
243  pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
244  pic->mb_mean = pic->mb_mean_buf->data;
245  }
246 
247  pic->mbskip_table = pic->mbskip_table_buf->data;
248  pic->qscale_table = pic->qscale_table_buf->data + 2 * mb_stride + 1;
249  pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * mb_stride + 1;
250 
251  if (pic->motion_val_buf[0]) {
252  for (i = 0; i < 2; i++) {
253  pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
254  pic->ref_index[i] = pic->ref_index_buf[i]->data;
255  }
256  }
257 
258  return 0;
259 fail:
260  av_log(avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
261  ff_mpeg_unref_picture(avctx, pic);
263  return AVERROR(ENOMEM);
264 }
265 
270 {
271  pic->tf.f = pic->f;
272  /* WM Image / Screen codecs allocate internal buffers with different
273  * dimensions / colorspaces; ignore user-defined callbacks for these. */
274  if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
275  avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
276  avctx->codec_id != AV_CODEC_ID_MSS2)
277  ff_thread_release_buffer(avctx, &pic->tf);
278  else if (pic->f)
279  av_frame_unref(pic->f);
280 
282 
283  if (pic->needs_realloc)
285 }
286 
288 {
289  int i;
290 
291 #define UPDATE_TABLE(table) \
292 do { \
293  if (src->table && \
294  (!dst->table || dst->table->buffer != src->table->buffer)) { \
295  av_buffer_unref(&dst->table); \
296  dst->table = av_buffer_ref(src->table); \
297  if (!dst->table) { \
298  ff_free_picture_tables(dst); \
299  return AVERROR(ENOMEM); \
300  } \
301  } \
302 } while (0)
303 
304  UPDATE_TABLE(mb_var_buf);
305  UPDATE_TABLE(mc_mb_var_buf);
306  UPDATE_TABLE(mb_mean_buf);
307  UPDATE_TABLE(mbskip_table_buf);
308  UPDATE_TABLE(qscale_table_buf);
309  UPDATE_TABLE(mb_type_buf);
310  for (i = 0; i < 2; i++) {
311  UPDATE_TABLE(motion_val_buf[i]);
312  UPDATE_TABLE(ref_index_buf[i]);
313  }
314 
315  dst->mb_var = src->mb_var;
316  dst->mc_mb_var = src->mc_mb_var;
317  dst->mb_mean = src->mb_mean;
318  dst->mbskip_table = src->mbskip_table;
319  dst->qscale_table = src->qscale_table;
320  dst->mb_type = src->mb_type;
321  for (i = 0; i < 2; i++) {
322  dst->motion_val[i] = src->motion_val[i];
323  dst->ref_index[i] = src->ref_index[i];
324  }
325 
326  return 0;
327 }
328 
330 {
331  int ret;
332 
333  av_assert0(!dst->f->buf[0]);
334  av_assert0(src->f->buf[0]);
335 
336  src->tf.f = src->f;
337  dst->tf.f = dst->f;
338  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
339  if (ret < 0)
340  goto fail;
341 
342  ret = ff_update_picture_tables(dst, src);
343  if (ret < 0)
344  goto fail;
345 
346  if (src->hwaccel_picture_private) {
348  if (!dst->hwaccel_priv_buf)
349  goto fail;
351  }
352 
353  dst->field_picture = src->field_picture;
354  dst->mb_var_sum = src->mb_var_sum;
355  dst->mc_mb_var_sum = src->mc_mb_var_sum;
356  dst->b_frame_score = src->b_frame_score;
357  dst->needs_realloc = src->needs_realloc;
358  dst->reference = src->reference;
359  dst->shared = src->shared;
360 
361  memcpy(dst->encoding_error, src->encoding_error,
362  sizeof(dst->encoding_error));
363 
364  return 0;
365 fail:
366  ff_mpeg_unref_picture(avctx, dst);
367  return ret;
368 }
369 
370 static inline int pic_is_unused(Picture *pic)
371 {
372  if (!pic->f->buf[0])
373  return 1;
374  if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
375  return 1;
376  return 0;
377 }
378 
379 static int find_unused_picture(Picture *picture, int shared)
380 {
381  int i;
382 
383  if (shared) {
384  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
385  if (!picture[i].f->buf[0])
386  return i;
387  }
388  } else {
389  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
390  if (pic_is_unused(&picture[i]))
391  return i;
392  }
393  }
394 
395  return AVERROR_INVALIDDATA;
396 }
397 
398 int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
399 {
400  int ret = find_unused_picture(picture, shared);
401 
402  if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
403  if (picture[ret].needs_realloc) {
404  picture[ret].needs_realloc = 0;
405  ff_free_picture_tables(&picture[ret]);
406  ff_mpeg_unref_picture(avctx, &picture[ret]);
407  }
408  }
409  return ret;
410 }
411 
413 {
414  int i;
415 
422 
423  for (i = 0; i < 2; i++) {
425  av_buffer_unref(&pic->ref_index_buf[i]);
426  }
427 }
uint8_t * scratchpad
data area for the ME algo, so that the ME does not need to malloc/free.
Definition: motion_est.h:47
const struct AVCodec * codec
Definition: avcodec.h:1418
static int alloc_picture_tables(Picture *pic, int encoding, int out_format, int mb_stride, int mb_height, int b8_stride)
Definition: mpegpicture.c:168
int8_t * ref_index[2]
Definition: mpegpicture.h:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
AVBufferRef * mb_var_buf
Definition: mpegpicture.h:64
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:106
uint8_t * mb_mean
Table for MB luminance.
Definition: mpegpicture.h:71
uint8_t * edge_emu_buffer
temporary buffer for if MVs point to out-of-frame data
Definition: mpegpicture.h:36
AVFrame * f
Definition: thread.h:36
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:308
uint16_t * mb_var
Table for MB variances.
Definition: mpegpicture.h:65
static int pic_is_unused(Picture *pic)
Definition: mpegpicture.c:370
int needs_realloc
Picture needs to be reallocated (eg due to a frame size change)
Definition: mpegpicture.h:82
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int field_picture
whether or not the picture was encoded in separate fields
Definition: mpegpicture.h:76
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
static int alloc_frame_buffer(AVCodecContext *avctx, Picture *pic, MotionEstContext *me, ScratchpadContext *sc, int chroma_x_shift, int chroma_y_shift, int linesize, int uvlinesize)
Allocate a frame buffer.
Definition: mpegpicture.c:85
int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
Definition: mpegpicture.c:329
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
static int find_unused_picture(Picture *picture, int shared)
Definition: mpegpicture.c:379
int b_frame_score
Definition: mpegpicture.h:81
int av_codec_is_encoder(const AVCodec *codec)
Definition: utils.c:96
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2696
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static int make_tables_writable(Picture *pic)
Definition: mpegpicture.c:31
Motion estimation context.
Definition: motion_est.h:42
void ff_free_picture_tables(Picture *pic)
Definition: mpegpicture.c:412
int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
Definition: mpegpicture.c:398
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:2655
AVBufferRef * mb_type_buf
Definition: mpegpicture.h:55
#define FFALIGN(x, a)
Definition: macros.h:48
#define UPDATE_TABLE(table)
AVBufferRef * mb_mean_buf
Definition: mpegpicture.h:70
#define r
Definition: input.c:51
uint64_t encoding_error[4]
Definition: mpegpicture.h:87
#define EDGE_WIDTH
Definition: mpegpicture.h:33
ThreadFrame tf
Definition: mpegpicture.h:47
#define src
Definition: vp8dsp.c:254
int width
width and height of the video frame
Definition: frame.h:179
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
uint8_t * mbskip_table
Definition: mpegpicture.h:59
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
uint8_t * rd_scratchpad
scratchpad for rate distortion mb decision
Definition: mpegpicture.h:37
#define AVERROR(e)
Definition: error.h:43
#define MAX_PICTURE_COUNT
Definition: mpegpicture.h:32
int reference
Definition: mpegpicture.h:84
simple assert() macros that are a bit more flexible than ISO C assert().
#define fail()
Definition: checkasm.h:80
AVBufferRef * hwaccel_priv_buf
Definition: mpegpicture.h:73
AVBufferRef * motion_val_buf[2]
Definition: mpegpicture.h:52
int width
picture width / height.
Definition: avcodec.h:1580
int16_t(*[2] motion_val)[2]
Definition: mpegpicture.h:53
Picture.
Definition: mpegpicture.h:45
void * hwaccel_picture_private
Hardware accelerator private data.
Definition: mpegpicture.h:74
#define FFABS(a)
Definition: common.h:61
#define DELAYED_PIC_REF
Value of Picture.reference when Picture is not a reference picture, but is held for delayed output...
Definition: mpegutils.h:45
if(ac->has_optimized_func)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:191
uint16_t * mc_mb_var
Table for motion compensated MB variances.
Definition: mpegpicture.h:68
AVBufferRef * qscale_table_buf
Definition: mpegpicture.h:49
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
Definition: utils.c:514
Libavcodec external API header.
int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me, ScratchpadContext *sc, int shared, int encoding, int chroma_x_shift, int chroma_y_shift, int out_format, int mb_stride, int mb_height, int b8_stride, ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
Allocate a Picture.
Definition: mpegpicture.c:211
enum AVCodecID codec_id
Definition: avcodec.h:1426
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:1409
uint8_t * data
The data buffer.
Definition: buffer.h:89
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:82
int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me, ScratchpadContext *sc, int linesize)
Definition: mpegpicture.c:56
struct AVFrame * f
Definition: mpegpicture.h:46
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:302
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
int8_t * qscale_table
Definition: mpegpicture.h:50
void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
Deallocate a picture.
Definition: mpegpicture.c:269
#define MAKE_WRITABLE(table)
common internal and external API header
AVBufferRef * mbskip_table_buf
Definition: mpegpicture.h:58
int shared
Definition: mpegpicture.h:85
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
uint8_t * b_scratchpad
scratchpad used for writing into write only buffers
Definition: mpegpicture.h:39
int ff_update_picture_tables(Picture *dst, Picture *src)
Definition: mpegpicture.c:287
uint8_t * obmc_scratchpad
Definition: mpegpicture.h:38
int frame_priv_data_size
Size of per-frame hardware accelerator private data.
Definition: avcodec.h:3334
AVBufferRef * mc_mb_var_buf
Definition: mpegpicture.h:67
int mb_var_sum
sum of MB variance for current frame
Definition: mpegpicture.h:78
int height
Definition: frame.h:179
int mc_mb_var_sum
motion compensated MB variance for current frame
Definition: mpegpicture.h:79
uint32_t * mb_type
types and macros are defined in mpegutils.h
Definition: mpegpicture.h:56
uint8_t * temp
Definition: motion_est.h:51
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1183
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)
Definition: internal.h:129
AVBufferRef * ref_index_buf[2]
Definition: mpegpicture.h:61