FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
h264_picture.c
Go to the documentation of this file.
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... decoder
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * H.264 / AVC / MPEG4 part10 codec.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27 
28 #include "libavutil/avassert.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/timer.h"
31 #include "internal.h"
32 #include "cabac.h"
33 #include "cabac_functions.h"
34 #include "error_resilience.h"
35 #include "avcodec.h"
36 #include "h264.h"
37 #include "h264data.h"
38 #include "h264chroma.h"
39 #include "h264_mvpred.h"
40 #include "golomb.h"
41 #include "mathops.h"
42 #include "mpegutils.h"
43 #include "rectangle.h"
44 #include "thread.h"
45 #include "vdpau_compat.h"
46 
48 {
49  int off = offsetof(H264Picture, tf) + sizeof(pic->tf);
50  int i;
51 
52  if (!pic->f || !pic->f->buf[0])
53  return;
54 
57 
60  for (i = 0; i < 2; i++) {
63  }
64 
65  memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
66 }
67 
69 {
70  int ret, i;
71 
72  av_assert0(!dst->f->buf[0]);
73  av_assert0(src->f->buf[0]);
74 
75  src->tf.f = src->f;
76  dst->tf.f = dst->f;
77  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
78  if (ret < 0)
79  goto fail;
80 
83  if (!dst->qscale_table_buf || !dst->mb_type_buf)
84  goto fail;
85  dst->qscale_table = src->qscale_table;
86  dst->mb_type = src->mb_type;
87 
88  for (i = 0; i < 2; i++) {
89  dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]);
90  dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]);
91  if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i])
92  goto fail;
93  dst->motion_val[i] = src->motion_val[i];
94  dst->ref_index[i] = src->ref_index[i];
95  }
96 
97  if (src->hwaccel_picture_private) {
99  if (!dst->hwaccel_priv_buf)
100  goto fail;
102  }
103 
104  for (i = 0; i < 2; i++)
105  dst->field_poc[i] = src->field_poc[i];
106 
107  memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
108  memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
109 
110  dst->poc = src->poc;
111  dst->frame_num = src->frame_num;
112  dst->mmco_reset = src->mmco_reset;
113  dst->pic_id = src->pic_id;
114  dst->long_ref = src->long_ref;
115  dst->mbaff = src->mbaff;
116  dst->field_picture = src->field_picture;
117  dst->reference = src->reference;
118  dst->crop = src->crop;
119  dst->crop_left = src->crop_left;
120  dst->crop_top = src->crop_top;
121  dst->recovered = src->recovered;
122  dst->invalid_gap = src->invalid_gap;
124 
125  return 0;
126 fail:
127  ff_h264_unref_picture(h, dst);
128  return ret;
129 }
130 
132 {
133 #if CONFIG_ERROR_RESILIENCE
134  int i;
135 
136  memset(dst, 0, sizeof(*dst));
137 
138  if (!src)
139  return;
140 
141  dst->f = src->f;
142  dst->tf = &src->tf;
143 
144  for (i = 0; i < 2; i++) {
145  dst->motion_val[i] = src->motion_val[i];
146  dst->ref_index[i] = src->ref_index[i];
147  }
148 
149  dst->mb_type = src->mb_type;
150  dst->field_picture = src->field_picture;
151 #endif /* CONFIG_ERROR_RESILIENCE */
152 }
153 
155 {
156  AVCodecContext *const avctx = h->avctx;
157  int err = 0;
158  h->mb_y = 0;
159 
160 #if FF_API_CAP_VDPAU
161  if (CONFIG_H264_VDPAU_DECODER &&
164 #endif
165 
166  if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
167  if (!h->droppable) {
169  h->prev_poc_msb = h->poc_msb;
170  h->prev_poc_lsb = h->poc_lsb;
171  }
173  h->prev_frame_num = h->frame_num;
174  }
175 
176  if (avctx->hwaccel) {
177  err = avctx->hwaccel->end_frame(avctx);
178  if (err < 0)
179  av_log(avctx, AV_LOG_ERROR,
180  "hardware accelerator failed to decode picture\n");
181  }
182 
183 #if FF_API_CAP_VDPAU
184  if (CONFIG_H264_VDPAU_DECODER &&
187 #endif
188 
189 #if CONFIG_ERROR_RESILIENCE
190  av_assert0(sl == h->slice_ctx);
191  /*
192  * FIXME: Error handling code does not seem to support interlaced
193  * when slices span multiple rows
194  * The ff_er_add_slice calls don't work right for bottom
195  * fields; they cause massive erroneous error concealing
196  * Error marking covers both fields (top and bottom).
197  * This causes a mismatched s->error_count
198  * and a bad error table. Further, the error count goes to
199  * INT_MAX when called for bottom field, because mb_y is
200  * past end by one (callers fault) and resync_mb_y != 0
201  * causes problems for the first MB line, too.
202  */
203  if (!FIELD_PICTURE(h) && h->current_slice && !h->sps.new && h->enable_er) {
204  int use_last_pic = h->last_pic_for_ec.f->buf[0] && !sl->ref_count[0];
205 
207 
208  if (use_last_pic) {
210  sl->ref_list[0][0].parent = &h->last_pic_for_ec;
211  memcpy(sl->ref_list[0][0].data, h->last_pic_for_ec.f->data, sizeof(sl->ref_list[0][0].data));
212  memcpy(sl->ref_list[0][0].linesize, h->last_pic_for_ec.f->linesize, sizeof(sl->ref_list[0][0].linesize));
214  } else if (sl->ref_count[0]) {
215  ff_h264_set_erpic(&sl->er.last_pic, sl->ref_list[0][0].parent);
216  } else
218 
219  if (sl->ref_count[1])
220  ff_h264_set_erpic(&sl->er.next_pic, sl->ref_list[1][0].parent);
221 
222  sl->er.ref_count = sl->ref_count[0];
223 
224  ff_er_frame_end(&sl->er);
225  if (use_last_pic)
226  memset(&sl->ref_list[0][0], 0, sizeof(sl->ref_list[0][0]));
227  }
228 #endif /* CONFIG_ERROR_RESILIENCE */
229 
230  if (!in_setup && !h->droppable)
233  emms_c();
234 
235  h->current_slice = 0;
236 
237  return err;
238 }
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:1511
void ff_vdpau_h264_picture_complete(H264Context *h)
int long_ref
1->long term reference 0->short term reference
Definition: h264.h:335
int sei_recovery_frame_cnt
Definition: h264.h:344
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:124
misc image utilities
AVFrame * f
Definition: thread.h:36
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:441
MMCO mmco[MAX_MMCO_COUNT]
memory management control operations buffer.
Definition: h264.h:678
void ff_er_frame_end(ERContext *s)
int mb_y
Definition: h264.h:618
AVBufferRef * mb_type_buf
Definition: h264.h:319
int crop
Definition: h264.h:346
int16_t(*[2] motion_val)[2]
Definition: h264.h:317
int mmco_index
Definition: h264.h:679
uint32_t * mb_type
ERPicture last_pic
H264Context.
Definition: h264.h:517
AVFrame * f
Definition: h264.h:310
int prev_poc_msb
poc_msb of the last reference pic for POC type 0
Definition: h264.h:651
int picture_structure
Definition: h264.h:590
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264.h:460
#define tf
Definition: regdef.h:73
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2922
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
int prev_frame_num_offset
for POC type 2
Definition: h264.h:654
void ff_h264_set_erpic(ERPicture *dst, H264Picture *src)
Definition: h264_picture.c:131
int field_picture
whether or not picture was encoded in separate fields
Definition: h264.h:339
int poc
frame POC
Definition: h264.h:329
Multithreading support functions.
int invalid_gap
Definition: h264.h:343
ERPicture cur_pic
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:34
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:3762
#define AV_CODEC_CAP_HWACCEL_VDPAU
Codec can export data for HW decoding (VDPAU).
Definition: avcodec.h:893
AVBufferRef * qscale_table_buf
Definition: h264.h:313
H264Picture * parent
Definition: h264.h:359
high precision timer, useful to profile code
int recovered
picture at IDR or recovery point + recovery count
Definition: h264.h:342
#define av_log(a,...)
H.264 / AVC / MPEG4 part10 codec.
int frame_num
Definition: h264.h:650
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
ERContext er
Definition: h264.h:365
void * hwaccel_picture_private
hardware accelerator private data
Definition: h264.h:323
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:3052
int capabilities
Codec capabilities.
Definition: avcodec.h:3491
int ref_poc[2][2][32]
POCs of the frames/fields used as reference (FIXME need per slice)
Definition: h264.h:336
ThreadFrame tf
Definition: h264.h:311
simple assert() macros that are a bit more flexible than ISO C assert().
ThreadFrame * tf
int frame_num
frame_num (raw frame_num from slice header)
Definition: h264.h:330
Libavcodec external API header.
#define fail()
Definition: checkasm.h:57
int new
flag to keep track if the decoder context needs re-init due to changed SPS
Definition: h264.h:231
AVBufferRef * hwaccel_priv_buf
Definition: h264.h:322
int crop_left
Definition: h264.h:347
useful rectangle filling function
uint8_t * data[3]
Definition: h264.h:352
AVBufferRef * motion_val_buf[2]
Definition: h264.h:316
int frame_num_offset
for POC type 2
Definition: h264.h:653
int reference
Definition: h264.h:341
#define FIELD_PICTURE(h)
Definition: h264.h:74
uint32_t * mb_type
Definition: h264.h:320
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
SPS sps
current sps
Definition: h264.h:576
Context Adaptive Binary Arithmetic Coder inline functions.
H264SliceContext * slice_ctx
Definition: h264.h:531
int poc_lsb
Definition: h264.h:646
int reference
Definition: h264.h:355
int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count)
Execute the reference picture marking (memory management control operations).
Definition: h264_refs.c:616
AVCodecContext * avctx
Definition: h264.h:519
AVS_Value src
Definition: avisynth_c.h:482
H264 / AVC / MPEG4 part10 codec data table
int prev_frame_num
frame_num of the last pic for POC type 1/2
Definition: h264.h:655
ERPicture next_pic
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
int poc_msb
Definition: h264.h:647
int field_poc[2]
top/bottom POC
Definition: h264.h:328
main external API structure.
Definition: avcodec.h:1502
uint8_t * data
The data buffer.
Definition: buffer.h:89
void ff_vdpau_h264_set_reference_frames(H264Context *h)
int8_t * qscale_table
Definition: h264.h:314
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:3044
int8_t * ref_index[2]
Definition: h264.h:326
int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
Definition: h264_picture.c:68
int mmco_reset
MMCO_RESET set this 1.
Definition: h264.h:331
H264Picture * cur_pic_ptr
Definition: h264.h:527
int enable_er
Definition: h264.h:828
int8_t * ref_index[2]
int linesize[3]
Definition: h264.h:353
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
int mbaff
1 -> MBAFF frame 0-> not MBAFF
Definition: h264.h:338
int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
Definition: h264_picture.c:154
int pic_id
pic_num (short -> no wrap version of pic_num, pic_num & max_pic_num; long -> long_pic_num) ...
Definition: h264.h:333
common internal api header.
H.264 / AVC / MPEG4 part10 motion vector predicion.
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
AVFrame * f
int prev_poc_lsb
poc_lsb of the last reference pic for POC type 0
Definition: h264.h:652
int16_t(*[2] motion_val)[2]
int current_slice
current slice number, used to initialize slice_num of each thread/context
Definition: h264.h:692
H264Ref ref_list[2][48]
0..15: frame refs, 16..47: mbaff field refs.
Definition: h264.h:462
AVBufferRef * ref_index_buf[2]
Definition: h264.h:325
H264Picture last_pic_for_ec
Definition: h264.h:529
int crop_top
Definition: h264.h:348
int ref_count[2][2]
number of entries in ref_poc (FIXME need per slice)
Definition: h264.h:337
int(* end_frame)(AVCodecContext *avctx)
Called at the end of each frame or field picture.
Definition: avcodec.h:3665
exp golomb vlc stuff
void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
Definition: h264_picture.c:47
int droppable
Definition: h264.h:548
Context Adaptive Binary Arithmetic Coder.