FFmpeg
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 / MPEG-4 part10 codec.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27 
28 #include "libavutil/avassert.h"
29 #include "libavutil/imgutils.h"
30 #include "internal.h"
31 #include "cabac.h"
32 #include "cabac_functions.h"
33 #include "error_resilience.h"
34 #include "avcodec.h"
35 #include "h264dec.h"
36 #include "h264data.h"
37 #include "h264chroma.h"
38 #include "h264_mvpred.h"
39 #include "mathops.h"
40 #include "mpegutils.h"
41 #include "rectangle.h"
42 #include "thread.h"
43 
45 {
46  int off = offsetof(H264Picture, tf_grain) + sizeof(pic->tf_grain);
47  int i;
48 
49  if (!pic->f || !pic->f->buf[0])
50  return;
51 
52  ff_thread_release_buffer(h->avctx, &pic->tf);
53  ff_thread_release_buffer(h->avctx, &pic->tf_grain);
55 
58  av_buffer_unref(&pic->pps_buf);
59  for (i = 0; i < 2; i++) {
62  }
63 
64  memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
65 }
66 
68 {
69  dst->qscale_table = src->qscale_table;
70  dst->mb_type = src->mb_type;
71  dst->pps = src->pps;
72 
73  for (int i = 0; i < 2; i++) {
74  dst->motion_val[i] = src->motion_val[i];
75  dst->ref_index[i] = src->ref_index[i];
76  }
77 
78  for (int i = 0; i < 2; i++)
79  dst->field_poc[i] = src->field_poc[i];
80 
81  memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
82  memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
83 
84  dst->poc = src->poc;
85  dst->frame_num = src->frame_num;
86  dst->mmco_reset = src->mmco_reset;
87  dst->long_ref = src->long_ref;
88  dst->mbaff = src->mbaff;
89  dst->field_picture = src->field_picture;
90  dst->reference = src->reference;
91  dst->recovered = src->recovered;
92  dst->invalid_gap = src->invalid_gap;
93  dst->sei_recovery_frame_cnt = src->sei_recovery_frame_cnt;
94  dst->mb_width = src->mb_width;
95  dst->mb_height = src->mb_height;
96  dst->mb_stride = src->mb_stride;
97  dst->needs_fg = src->needs_fg;
98 }
99 
101 {
102  int ret, i;
103 
104  av_assert0(!dst->f->buf[0]);
105  av_assert0(src->f->buf[0]);
106  av_assert0(src->tf.f == src->f);
107 
108  dst->tf.f = dst->f;
109  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
110  if (ret < 0)
111  goto fail;
112 
113  if (src->needs_fg) {
114  av_assert0(src->tf_grain.f == src->f_grain);
115  dst->tf_grain.f = dst->f_grain;
116  ret = ff_thread_ref_frame(&dst->tf_grain, &src->tf_grain);
117  if (ret < 0)
118  goto fail;
119  }
120 
121  dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf);
122  dst->mb_type_buf = av_buffer_ref(src->mb_type_buf);
123  dst->pps_buf = av_buffer_ref(src->pps_buf);
124  if (!dst->qscale_table_buf || !dst->mb_type_buf || !dst->pps_buf) {
125  ret = AVERROR(ENOMEM);
126  goto fail;
127  }
128 
129  for (i = 0; i < 2; i++) {
130  dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]);
131  dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]);
132  if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i]) {
133  ret = AVERROR(ENOMEM);
134  goto fail;
135  }
136  }
137 
138  if (src->hwaccel_picture_private) {
139  dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
140  if (!dst->hwaccel_priv_buf) {
141  ret = AVERROR(ENOMEM);
142  goto fail;
143  }
145  }
146 
148 
149  return 0;
150 fail:
151  ff_h264_unref_picture(h, dst);
152  return ret;
153 }
154 
156 {
157  int ret, i;
158 
159  if (!src->f || !src->f->buf[0]) {
160  ff_h264_unref_picture(h, dst);
161  return 0;
162  }
163 
164  av_assert0(src->tf.f == src->f);
165 
166  dst->tf.f = dst->f;
167  ff_thread_release_buffer(h->avctx, &dst->tf);
168  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
169  if (ret < 0)
170  goto fail;
171 
172  if (src->needs_fg) {
173  av_assert0(src->tf_grain.f == src->f_grain);
174  dst->tf_grain.f = dst->f_grain;
175  ff_thread_release_buffer(h->avctx, &dst->tf_grain);
176  ret = ff_thread_ref_frame(&dst->tf_grain, &src->tf_grain);
177  if (ret < 0)
178  goto fail;
179  }
180 
181  ret = av_buffer_replace(&dst->qscale_table_buf, src->qscale_table_buf);
182  ret |= av_buffer_replace(&dst->mb_type_buf, src->mb_type_buf);
183  ret |= av_buffer_replace(&dst->pps_buf, src->pps_buf);
184  if (ret < 0)
185  goto fail;
186 
187  for (i = 0; i < 2; i++) {
188  ret = av_buffer_replace(&dst->motion_val_buf[i], src->motion_val_buf[i]);
189  ret |= av_buffer_replace(&dst->ref_index_buf[i], src->ref_index_buf[i]);
190  if (ret < 0)
191  goto fail;
192  }
193 
194  ret = av_buffer_replace(&dst->hwaccel_priv_buf, src->hwaccel_priv_buf);
195  if (ret < 0)
196  goto fail;
197 
198  dst->hwaccel_picture_private = src->hwaccel_picture_private;
199 
201 
202  return 0;
203 fail:
204  ff_h264_unref_picture(h, dst);
205  return ret;
206 }
207 
209 {
210 #if CONFIG_ERROR_RESILIENCE
211  int i;
212 
213  memset(dst, 0, sizeof(*dst));
214 
215  if (!src)
216  return;
217 
218  dst->f = src->f;
219  dst->tf = &src->tf;
220 
221  for (i = 0; i < 2; i++) {
222  dst->motion_val[i] = src->motion_val[i];
223  dst->ref_index[i] = src->ref_index[i];
224  }
225 
226  dst->mb_type = src->mb_type;
227  dst->field_picture = src->field_picture;
228 #endif /* CONFIG_ERROR_RESILIENCE */
229 }
230 
232 {
233  AVCodecContext *const avctx = h->avctx;
234  H264Picture *cur = h->cur_pic_ptr;
235  int err = 0;
236  h->mb_y = 0;
237 
238  if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
239  if (!h->droppable) {
241  h->poc.prev_poc_msb = h->poc.poc_msb;
242  h->poc.prev_poc_lsb = h->poc.poc_lsb;
243  }
244  h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
245  h->poc.prev_frame_num = h->poc.frame_num;
246  }
247 
248  if (avctx->hwaccel) {
249  err = avctx->hwaccel->end_frame(avctx);
250  if (err < 0)
251  av_log(avctx, AV_LOG_ERROR,
252  "hardware accelerator failed to decode picture\n");
253  } else if (!in_setup && cur->needs_fg && (!FIELD_PICTURE(h) || !h->first_field)) {
255 
256  err = AVERROR_INVALIDDATA;
257  if (sd) // a decoding error may have happened before the side data could be allocated
258  err = ff_h274_apply_film_grain(cur->f_grain, cur->f, &h->h274db,
259  (AVFilmGrainParams *) sd->data);
260  if (err < 0) {
261  av_log(h->avctx, AV_LOG_WARNING, "Failed synthesizing film "
262  "grain, ignoring: %s\n", av_err2str(err));
263  cur->needs_fg = 0;
264  err = 0;
265  }
266  }
267 
268  if (!in_setup && !h->droppable)
269  ff_thread_report_progress(&cur->tf, INT_MAX,
270  h->picture_structure == PICT_BOTTOM_FIELD);
271  emms_c();
272 
273  h->current_slice = 0;
274 
275  return err;
276 }
AVCodecContext::hwaccel
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1359
ff_h264_unref_picture
void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
Definition: h264_picture.c:44
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
cabac.h
ERPicture::tf
ThreadFrame * tf
Definition: error_resilience.h:43
H264Picture::poc
int poc
frame POC
Definition: h264dec.h:153
H264Picture::f
AVFrame * f
Definition: h264dec.h:131
ff_thread_ref_frame
int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
Definition: utils.c:885
av_frame_get_side_data
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:617
ff_h264_replace_picture
int ff_h264_replace_picture(H264Context *h, H264Picture *dst, const H264Picture *src)
Definition: h264_picture.c:155
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
H264Picture::ref_count
int ref_count[2][2]
number of entries in ref_poc (FIXME need per slice)
Definition: h264dec.h:161
H264Picture::ref_index
int8_t * ref_index[2]
Definition: h264dec.h:150
ff_h264_set_erpic
void ff_h264_set_erpic(ERPicture *dst, H264Picture *src)
Definition: h264_picture.c:208
ERPicture::ref_index
int8_t * ref_index[2]
Definition: error_resilience.h:47
AV_FRAME_DATA_FILM_GRAIN_PARAMS
@ AV_FRAME_DATA_FILM_GRAIN_PARAMS
Film grain parameters for a frame, described by AVFilmGrainParams.
Definition: frame.h:183
H264Picture::pps
const PPS * pps
Definition: h264dec.h:172
internal.h
H264Picture::qscale_table
int8_t * qscale_table
Definition: h264dec.h:138
PICT_BOTTOM_FIELD
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:37
H264Picture::ref_index_buf
AVBufferRef * ref_index_buf[2]
Definition: h264dec.h:149
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
mpegutils.h
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:477
H264Picture::invalid_gap
int invalid_gap
Definition: h264dec.h:167
H264Picture::pps_buf
AVBufferRef * pps_buf
Definition: h264dec.h:171
thread.h
ThreadFrame::f
AVFrame * f
Definition: thread.h:35
ERPicture
Definition: error_resilience.h:41
h264_mvpred.h
H264Picture::frame_num
int frame_num
frame_num (raw frame_num from slice header)
Definition: h264dec.h:154
H264SliceContext
Definition: h264dec.h:189
H264Picture::mmco_reset
int mmco_reset
MMCO_RESET set this 1.
Definition: h264dec.h:155
fail
#define fail()
Definition: checkasm.h:127
H264Picture::ref_poc
int ref_poc[2][2][32]
POCs of the frames/fields used as reference (FIXME need per slice)
Definition: h264dec.h:160
ERPicture::motion_val
int16_t(*[2] motion_val)[2]
Definition: error_resilience.h:46
H264Picture::mb_stride
int mb_stride
Definition: h264dec.h:175
H264Picture::f_grain
AVFrame * f_grain
Definition: h264dec.h:134
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
ff_thread_report_progress
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
Definition: pthread_frame.c:588
H264Picture::mbaff
int mbaff
1 -> MBAFF frame 0-> not MBAFF
Definition: h264dec.h:162
ERPicture::field_picture
int field_picture
Definition: error_resilience.h:50
FIELD_PICTURE
#define FIELD_PICTURE(h)
Definition: h264dec.h:76
ff_h264_execute_ref_pic_marking
int ff_h264_execute_ref_pic_marking(H264Context *h)
Execute the reference picture marking (memory management control operations).
Definition: h264_refs.c:610
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
h264data.h
ERPicture::f
AVFrame * f
Definition: error_resilience.h:42
H264Picture::sei_recovery_frame_cnt
int sei_recovery_frame_cnt
Definition: h264dec.h:168
ERPicture::mb_type
uint32_t * mb_type
Definition: error_resilience.h:49
H264Picture::motion_val_buf
AVBufferRef * motion_val_buf[2]
Definition: h264dec.h:140
av_buffer_unref
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:139
AVHWAccel::end_frame
int(* end_frame)(AVCodecContext *avctx)
Called at the end of each frame or field picture.
Definition: avcodec.h:2140
src
#define src
Definition: vp8dsp.c:255
mathops.h
H264Picture::mb_height
int mb_height
Definition: h264dec.h:174
ff_thread_release_buffer
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
Definition: pthread_frame.c:1057
H264Picture::reference
int reference
Definition: h264dec.h:165
rectangle.h
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
H264Picture::tf
ThreadFrame tf
Definition: h264dec.h:132
H264Picture::mb_type
uint32_t * mb_type
Definition: h264dec.h:144
H264Picture::recovered
int recovered
picture at IDR or recovery point + recovery count
Definition: h264dec.h:166
AVFrameSideData::data
uint8_t * data
Definition: frame.h:211
h264chroma.h
AVFilmGrainParams
This structure describes how to handle film grain synthesis in video for specific codecs.
Definition: film_grain_params.h:216
FF_THREAD_FRAME
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1451
H264Picture::field_picture
int field_picture
whether or not picture was encoded in separate fields
Definition: h264dec.h:163
h264dec.h
ff_h274_apply_film_grain
int ff_h274_apply_film_grain(AVFrame *out_frame, const AVFrame *in_frame, H274FilmGrainDatabase *database, const AVFilmGrainParams *params)
Definition: h274.c:217
H264Picture::needs_fg
int needs_fg
whether picture needs film grain synthesis (see f_grain)
Definition: h264dec.h:169
H264Context
H264Context.
Definition: h264dec.h:350
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
cabac_functions.h
H264Picture::hwaccel_priv_buf
AVBufferRef * hwaccel_priv_buf
Definition: h264dec.h:146
h264_copy_picture_params
static void h264_copy_picture_params(H264Picture *dst, const H264Picture *src)
Definition: h264_picture.c:67
av_buffer_replace
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition: buffer.c:233
avcodec.h
ff_h264_ref_picture
int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
Definition: h264_picture.c:100
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:383
AVCodecContext::active_thread_type
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1459
H264Picture::field_poc
int field_poc[2]
top/bottom POC
Definition: h264dec.h:152
error_resilience.h
H264Picture::mb_width
int mb_width
Definition: h264dec.h:174
H264Picture
Definition: h264dec.h:130
H264Picture::tf_grain
ThreadFrame tf_grain
Definition: h264dec.h:135
ff_h264_field_end
int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
Definition: h264_picture.c:231
H264Picture::mb_type_buf
AVBufferRef * mb_type_buf
Definition: h264dec.h:143
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:209
H264Picture::hwaccel_picture_private
void * hwaccel_picture_private
hardware accelerator private data
Definition: h264dec.h:147
imgutils.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2038
H264Picture::qscale_table_buf
AVBufferRef * qscale_table_buf
Definition: h264dec.h:137
H264Picture::long_ref
int long_ref
1->long term reference 0->short term reference
Definition: h264dec.h:159
H264Picture::motion_val
int16_t(*[2] motion_val)[2]
Definition: h264dec.h:141