Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
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 
47 {
48  int off = offsetof(H264Picture, tf) + sizeof(pic->tf);
49  int i;
50 
51  if (!pic->f.buf[0])
52  return;
53 
56 
59  for (i = 0; i < 2; i++) {
62  }
63 
64  memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
65 }
66 
68 {
69  int ret, i;
70 
71  av_assert0(!dst->f.buf[0]);
72  av_assert0(src->f.buf[0]);
73 
74  src->tf.f = &src->f;
75  dst->tf.f = &dst->f;
76  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
77  if (ret < 0)
78  goto fail;
79 
82  if (!dst->qscale_table_buf || !dst->mb_type_buf)
83  goto fail;
84  dst->qscale_table = src->qscale_table;
85  dst->mb_type = src->mb_type;
86 
87  for (i = 0; i < 2; i++) {
88  dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]);
89  dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]);
90  if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i])
91  goto fail;
92  dst->motion_val[i] = src->motion_val[i];
93  dst->ref_index[i] = src->ref_index[i];
94  }
95 
96  if (src->hwaccel_picture_private) {
98  if (!dst->hwaccel_priv_buf)
99  goto fail;
101  }
102 
103  for (i = 0; i < 2; i++)
104  dst->field_poc[i] = src->field_poc[i];
105 
106  memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
107  memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
108 
109  dst->poc = src->poc;
110  dst->frame_num = src->frame_num;
111  dst->mmco_reset = src->mmco_reset;
112  dst->pic_id = src->pic_id;
113  dst->long_ref = src->long_ref;
114  dst->mbaff = src->mbaff;
115  dst->field_picture = src->field_picture;
116  dst->needs_realloc = src->needs_realloc;
117  dst->reference = src->reference;
118  dst->recovered = src->recovered;
119 
120  return 0;
121 fail:
122  ff_h264_unref_picture(h, dst);
123  return ret;
124 }
125 
126 #if CONFIG_ERROR_RESILIENCE
127 static void h264_set_erpic(ERPicture *dst, H264Picture *src)
128 {
129  int i;
130 
131  if (!src)
132  return;
133 
134  dst->f = &src->f;
135  dst->tf = &src->tf;
136 
137  for (i = 0; i < 2; i++) {
138  dst->motion_val[i] = src->motion_val[i];
139  dst->ref_index[i] = src->ref_index[i];
140  }
141 
142  dst->mb_type = src->mb_type;
143  dst->field_picture = src->field_picture;
144 }
145 #endif /* CONFIG_ERROR_RESILIENCE */
146 
147 int ff_h264_field_end(H264Context *h, int in_setup)
148 {
149  AVCodecContext *const avctx = h->avctx;
150  int err = 0;
151  h->mb_y = 0;
152 
153  if (!in_setup && !h->droppable)
156 
157  if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
158  if (!h->droppable) {
160  h->prev_poc_msb = h->poc_msb;
161  h->prev_poc_lsb = h->poc_lsb;
162  }
164  h->prev_frame_num = h->frame_num;
166  }
167 
168  if (avctx->hwaccel) {
169  if (avctx->hwaccel->end_frame(avctx) < 0)
170  av_log(avctx, AV_LOG_ERROR,
171  "hardware accelerator failed to decode picture\n");
172  }
173 
174 #if CONFIG_ERROR_RESILIENCE
175  /*
176  * FIXME: Error handling code does not seem to support interlaced
177  * when slices span multiple rows
178  * The ff_er_add_slice calls don't work right for bottom
179  * fields; they cause massive erroneous error concealing
180  * Error marking covers both fields (top and bottom).
181  * This causes a mismatched s->error_count
182  * and a bad error table. Further, the error count goes to
183  * INT_MAX when called for bottom field, because mb_y is
184  * past end by one (callers fault) and resync_mb_y != 0
185  * causes problems for the first MB line, too.
186  */
187  if (!FIELD_PICTURE(h)) {
188  h264_set_erpic(&h->er.cur_pic, h->cur_pic_ptr);
189  h264_set_erpic(&h->er.last_pic,
190  h->ref_count[0] ? &h->ref_list[0][0] : NULL);
191  h264_set_erpic(&h->er.next_pic,
192  h->ref_count[1] ? &h->ref_list[1][0] : NULL);
193  ff_er_frame_end(&h->er);
194  }
195 #endif /* CONFIG_ERROR_RESILIENCE */
196 
197  emms_c();
198 
199  h->current_slice = 0;
200 
201  return err;
202 }
int long_ref
1->long term reference 0->short term reference
Definition: h264.h:289
int ff_h264_field_end(H264Context *h, int in_setup)
Definition: h264_picture.c:147
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
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:393
MMCO mmco[MAX_MMCO_COUNT]
memory management control operations buffer.
Definition: h264.h:575
void ff_er_frame_end(ERContext *s)
int mb_y
Definition: h264.h:498
AVBufferRef * mb_type_buf
Definition: h264.h:273
int outputed_poc
Definition: h264.h:569
int16_t(*[2] motion_val)[2]
Definition: h264.h:271
int mmco_index
Definition: h264.h:576
uint32_t * mb_type
ERPicture last_pic
H264Context.
Definition: h264.h:303
int prev_poc_msb
poc_msb of the last reference pic for POC type 0
Definition: h264.h:545
struct AVFrame f
Definition: h264.h:264
int picture_structure
Definition: h264.h:419
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2448
#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:548
int field_picture
whether or not picture was encoded in separate fields
Definition: h264.h:293
int poc
frame POC
Definition: h264.h:283
Multithreading support functions.
#define emms_c()
Definition: internal.h:47
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition: h264.h:445
ERPicture cur_pic
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:34
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:2349
AVBufferRef * qscale_table_buf
Definition: h264.h:267
high precision timer, useful to profile code
int recovered
picture at IDR or recovery point + recovery count
Definition: h264.h:297
H.264 / AVC / MPEG4 part10 codec.
int frame_num
Definition: h264.h:544
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
void * hwaccel_picture_private
hardware accelerator private data
Definition: h264.h:277
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2575
int ref_poc[2][2][32]
POCs of the frames used as reference (FIXME need per slice)
Definition: h264.h:290
ThreadFrame tf
Definition: h264.h:265
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
ThreadFrame * tf
ERContext er
Definition: h264.h:312
int frame_num
frame_num (raw frame_num from slice header)
Definition: h264.h:284
AVBufferRef * hwaccel_priv_buf
Definition: h264.h:276
useful rectangle filling function
AVBufferRef * motion_val_buf[2]
Definition: h264.h:270
int frame_num_offset
for POC type 2
Definition: h264.h:547
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2567
int needs_realloc
picture needs to be reallocated (eg due to a frame size change)
Definition: h264.h:295
int reference
Definition: h264.h:296
#define FIELD_PICTURE(h)
Definition: h264.h:72
uint32_t * mb_type
Definition: h264.h:274
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
Context Adaptive Binary Arithmetic Coder inline functions.
H264Picture ref_list[2][48]
0..15: frame refs, 16..47: mbaff field refs.
Definition: h264.h:448
int poc_lsb
Definition: h264.h:540
NULL
Definition: eval.c:55
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:572
AVCodecContext * avctx
Definition: h264.h:304
Libavcodec external API header.
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:549
ERPicture next_pic
int next_outputed_poc
Definition: h264.h:570
int poc_msb
Definition: h264.h:541
int field_poc[2]
top/bottom POC
Definition: h264.h:282
main external API structure.
Definition: avcodec.h:1050
uint8_t * data
The data buffer.
Definition: buffer.h:89
int8_t * qscale_table
Definition: h264.h:268
int8_t * ref_index[2]
Definition: h264.h:280
int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
Definition: h264_picture.c:67
int mmco_reset
MMCO_RESET set this 1.
Definition: h264.h:285
H264Picture * cur_pic_ptr
Definition: h264.h:315
int8_t * ref_index[2]
int mbaff
1 -> MBAFF frame 0-> not MBAFF
Definition: h264.h:292
int pic_id
pic_num (short -> no wrap version of pic_num, pic_num & max_pic_num; long -> long_pic_num) ...
Definition: h264.h:287
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:546
int16_t(*[2] motion_val)[2]
int current_slice
current slice number, used to initalize slice_num of each thread/context
Definition: h264.h:593
AVBufferRef * ref_index_buf[2]
Definition: h264.h:279
int ref_count[2][2]
number of entries in ref_poc (FIXME need per slice)
Definition: h264.h:291
int(* end_frame)(AVCodecContext *avctx)
Called at the end of each frame or field picture.
Definition: avcodec.h:2996
exp golomb vlc stuff
void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
Definition: h264_picture.c:46
int droppable
Definition: h264.h:329
Context Adaptive Binary Arithmetic Coder.