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 "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 "h264dec.h"
37 #include "h264data.h"
38 #include "h264chroma.h"
39 #include "h264_mvpred.h"
40 #include "mathops.h"
41 #include "mpegutils.h"
42 #include "rectangle.h"
43 #include "thread.h"
44 
46 {
47  int off = offsetof(H264Picture, tf) + sizeof(pic->tf);
48  int i;
49 
50  if (!pic->f || !pic->f->buf[0])
51  return;
52 
53  ff_thread_release_buffer(h->avctx, &pic->tf);
55 
58  for (i = 0; i < 2; i++) {
61  }
62 
63  memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
64 }
65 
67 {
68  int ret, i;
69 
70  av_assert0(!dst->f->buf[0]);
71  av_assert0(src->f->buf[0]);
72  av_assert0(src->tf.f == src->f);
73 
74  dst->tf.f = dst->f;
75  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
76  if (ret < 0)
77  goto fail;
78 
79  dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf);
80  dst->mb_type_buf = av_buffer_ref(src->mb_type_buf);
81  if (!dst->qscale_table_buf || !dst->mb_type_buf) {
82  ret = AVERROR(ENOMEM);
83  goto fail;
84  }
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  ret = AVERROR(ENOMEM);
93  goto fail;
94  }
95  dst->motion_val[i] = src->motion_val[i];
96  dst->ref_index[i] = src->ref_index[i];
97  }
98 
99  if (src->hwaccel_picture_private) {
100  dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
101  if (!dst->hwaccel_priv_buf) {
102  ret = AVERROR(ENOMEM);
103  goto fail;
104  }
106  }
107 
108  for (i = 0; i < 2; i++)
109  dst->field_poc[i] = src->field_poc[i];
110 
111  memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
112  memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
113 
114  dst->poc = src->poc;
115  dst->frame_num = src->frame_num;
116  dst->mmco_reset = src->mmco_reset;
117  dst->long_ref = src->long_ref;
118  dst->mbaff = src->mbaff;
119  dst->field_picture = src->field_picture;
120  dst->reference = src->reference;
121  dst->recovered = src->recovered;
122  dst->invalid_gap = src->invalid_gap;
123  dst->sei_recovery_frame_cnt = src->sei_recovery_frame_cnt;
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 (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
161  if (!h->droppable) {
163  h->poc.prev_poc_msb = h->poc.poc_msb;
164  h->poc.prev_poc_lsb = h->poc.poc_lsb;
165  }
166  h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
167  h->poc.prev_frame_num = h->poc.frame_num;
168  }
169 
170  if (avctx->hwaccel) {
171  err = avctx->hwaccel->end_frame(avctx);
172  if (err < 0)
173  av_log(avctx, AV_LOG_ERROR,
174  "hardware accelerator failed to decode picture\n");
175  }
176 
177  if (!in_setup && !h->droppable)
178  ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
179  h->picture_structure == PICT_BOTTOM_FIELD);
180  emms_c();
181 
182  h->current_slice = 0;
183 
184  return err;
185 }
AVCodecContext::hwaccel
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2729
ff_h264_unref_picture
void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
Definition: h264_picture.c:45
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:148
H264Picture::f
AVFrame * f
Definition: h264dec.h:129
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:89
H264Picture::ref_count
int ref_count[2][2]
number of entries in ref_poc (FIXME need per slice)
Definition: h264dec.h:156
H264Picture::ref_index
int8_t * ref_index[2]
Definition: h264dec.h:145
ff_h264_set_erpic
void ff_h264_set_erpic(ERPicture *dst, H264Picture *src)
Definition: h264_picture.c:131
ERPicture::ref_index
int8_t * ref_index[2]
Definition: error_resilience.h:47
internal.h
H264Picture::qscale_table
int8_t * qscale_table
Definition: h264dec.h:133
PICT_BOTTOM_FIELD
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:38
H264Picture::ref_index_buf
AVBufferRef * ref_index_buf[2]
Definition: h264dec.h:144
mpegutils.h
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:486
H264Picture::invalid_gap
int invalid_gap
Definition: h264dec.h:162
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:149
H264SliceContext
Definition: h264dec.h:177
H264Picture::mmco_reset
int mmco_reset
MMCO_RESET set this 1.
Definition: h264dec.h:150
fail
#define fail()
Definition: checkasm.h:120
H264Picture::ref_poc
int ref_poc[2][2][32]
POCs of the frames/fields used as reference (FIXME need per slice)
Definition: h264dec.h:155
ERPicture::motion_val
int16_t(*[2] motion_val)[2]
Definition: error_resilience.h:46
src
#define src
Definition: vp8dsp.c:254
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:557
H264Picture::mbaff
int mbaff
1 -> MBAFF frame 0-> not MBAFF
Definition: h264dec.h:157
ERPicture::field_picture
int field_picture
Definition: error_resilience.h:50
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:163
ERPicture::mb_type
uint32_t * mb_type
Definition: error_resilience.h:49
H264Picture::motion_val_buf
AVBufferRef * motion_val_buf[2]
Definition: h264dec.h:135
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:125
AVHWAccel::end_frame
int(* end_frame)(AVCodecContext *avctx)
Called at the end of each frame or field picture.
Definition: avcodec.h:3750
mathops.h
timer.h
H264Picture::reference
int reference
Definition: h264dec.h:160
rectangle.h
H264Picture::tf
ThreadFrame tf
Definition: h264dec.h:130
H264Picture::mb_type
uint32_t * mb_type
Definition: h264dec.h:139
H264Picture::recovered
int recovered
picture at IDR or recovery point + recovery count
Definition: h264dec.h:161
h264chroma.h
FF_THREAD_FRAME
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2835
H264Picture::field_picture
int field_picture
whether or not picture was encoded in separate fields
Definition: h264dec.h:158
h264dec.h
H264Context
H264Context.
Definition: h264dec.h:337
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
uint8_t
uint8_t
Definition: audio_convert.c:194
cabac_functions.h
H264Picture::hwaccel_priv_buf
AVBufferRef * hwaccel_priv_buf
Definition: h264dec.h:141
avcodec.h
ff_h264_ref_picture
int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
Definition: h264_picture.c:66
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
AVCodecContext::active_thread_type
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2843
H264Picture::field_poc
int field_poc[2]
top/bottom POC
Definition: h264dec.h:147
error_resilience.h
H264Picture
Definition: h264dec.h:128
tf
#define tf
Definition: regdef.h:73
ff_h264_field_end
int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
Definition: h264_picture.c:154
av_buffer_ref
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
H264Picture::mb_type_buf
AVBufferRef * mb_type_buf
Definition: h264dec.h:138
ff_thread_ref_frame
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:1861
H264Picture::hwaccel_picture_private
void * hwaccel_picture_private
hardware accelerator private data
Definition: h264dec.h:142
imgutils.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
h
h
Definition: vp9dsp_template.c:2038
H264Picture::qscale_table_buf
AVBufferRef * qscale_table_buf
Definition: h264dec.h:132
H264Picture::long_ref
int long_ref
1->long term reference 0->short term reference
Definition: h264dec.h:154
H264Picture::motion_val
int16_t(*[2] motion_val)[2]
Definition: h264dec.h:136
ff_thread_release_buffer
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have add an so the codec calls ff_thread_report set AVCodecInternal allocate_progress The frames must then be freed with ff_thread_release_buffer(). Otherwise leave it at zero and decode directly into the user-supplied frames. Call ff_thread_report_progress() after some part of the current picture has decoded. A good place to put this is where draw_horiz_band() is called - add this if it isn 't called anywhere