FFmpeg
mpeg4video.c
Go to the documentation of this file.
1 /*
2  * MPEG-4 decoder / encoder common code
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "libavutil/thread.h"
24 
25 #include "mpegutils.h"
26 #include "mpegvideo.h"
27 #include "mpeg4video.h"
28 #include "mpeg4data.h"
29 
30 static av_cold void mpeg4_init_rl_intra(void)
31 {
32  static uint8_t mpeg4_rl_intra_table[2][2 * MAX_RUN + MAX_LEVEL + 3];
33  ff_rl_init(&ff_mpeg4_rl_intra, mpeg4_rl_intra_table);
34 }
35 
37 {
38  static AVOnce init_static_once = AV_ONCE_INIT;
39  ff_thread_once(&init_static_once, mpeg4_init_rl_intra);
40 }
41 
43 {
44  switch (s->pict_type) {
45  case AV_PICTURE_TYPE_I:
46  return 16;
47  case AV_PICTURE_TYPE_P:
48  case AV_PICTURE_TYPE_S:
49  return s->f_code + 15;
50  case AV_PICTURE_TYPE_B:
51  return FFMAX3(s->f_code, s->b_code, 2) + 15;
52  default:
53  return -1;
54  }
55 }
56 
58 {
59  int c_wrap, c_xy, l_wrap, l_xy;
60 
61  l_wrap = s->b8_stride;
62  l_xy = (2 * s->mb_y - 1) * l_wrap + s->mb_x * 2 - 1;
63  c_wrap = s->mb_stride;
64  c_xy = (s->mb_y - 1) * c_wrap + s->mb_x - 1;
65 
66  /* clean AC */
67  memset(s->ac_val[0] + l_xy, 0, (l_wrap * 2 + 1) * 16 * sizeof(int16_t));
68  memset(s->ac_val[1] + c_xy, 0, (c_wrap + 1) * 16 * sizeof(int16_t));
69  memset(s->ac_val[2] + c_xy, 0, (c_wrap + 1) * 16 * sizeof(int16_t));
70 
71  /* clean MV */
72  // we can't clear the MVs as they might be needed by a B-frame
73  s->last_mv[0][0][0] =
74  s->last_mv[0][0][1] =
75  s->last_mv[1][0][0] =
76  s->last_mv[1][0][1] = 0;
77 }
78 
79 #define tab_size ((signed)FF_ARRAY_ELEMS(s->direct_scale_mv[0]))
80 #define tab_bias (tab_size / 2)
81 
82 // used by MPEG-4 and rv10 decoder
84 {
85  int i;
86  for (i = 0; i < tab_size; i++) {
87  s->direct_scale_mv[0][i] = (i - tab_bias) * s->pb_time / s->pp_time;
88  s->direct_scale_mv[1][i] = (i - tab_bias) * (s->pb_time - s->pp_time) /
89  s->pp_time;
90  }
91 }
92 
93 static inline void ff_mpeg4_set_one_direct_mv(MpegEncContext *s, int mx,
94  int my, int i)
95 {
96  int xy = s->block_index[i];
97  uint16_t time_pp = s->pp_time;
98  uint16_t time_pb = s->pb_time;
99  int p_mx, p_my;
100 
101  p_mx = s->next_picture.motion_val[0][xy][0];
102  if ((unsigned)(p_mx + tab_bias) < tab_size) {
103  s->mv[0][i][0] = s->direct_scale_mv[0][p_mx + tab_bias] + mx;
104  s->mv[1][i][0] = mx ? s->mv[0][i][0] - p_mx
105  : s->direct_scale_mv[1][p_mx + tab_bias];
106  } else {
107  s->mv[0][i][0] = p_mx * time_pb / time_pp + mx;
108  s->mv[1][i][0] = mx ? s->mv[0][i][0] - p_mx
109  : p_mx * (time_pb - time_pp) / time_pp;
110  }
111  p_my = s->next_picture.motion_val[0][xy][1];
112  if ((unsigned)(p_my + tab_bias) < tab_size) {
113  s->mv[0][i][1] = s->direct_scale_mv[0][p_my + tab_bias] + my;
114  s->mv[1][i][1] = my ? s->mv[0][i][1] - p_my
115  : s->direct_scale_mv[1][p_my + tab_bias];
116  } else {
117  s->mv[0][i][1] = p_my * time_pb / time_pp + my;
118  s->mv[1][i][1] = my ? s->mv[0][i][1] - p_my
119  : p_my * (time_pb - time_pp) / time_pp;
120  }
121 }
122 
123 #undef tab_size
124 #undef tab_bias
125 
126 /**
127  * @return the mb_type
128  */
130 {
131  const int mb_index = s->mb_x + s->mb_y * s->mb_stride;
132  const int colocated_mb_type = s->next_picture.mb_type[mb_index];
133  uint16_t time_pp;
134  uint16_t time_pb;
135  int i;
136 
137  // FIXME avoid divides
138  // try special case with shifts for 1 and 3 B-frames?
139 
140  if (IS_8X8(colocated_mb_type)) {
141  s->mv_type = MV_TYPE_8X8;
142  for (i = 0; i < 4; i++)
143  ff_mpeg4_set_one_direct_mv(s, mx, my, i);
145  } else if (IS_INTERLACED(colocated_mb_type)) {
146  s->mv_type = MV_TYPE_FIELD;
147  for (i = 0; i < 2; i++) {
148  int field_select = s->next_picture.ref_index[0][4 * mb_index + 2 * i];
149  s->field_select[0][i] = field_select;
150  s->field_select[1][i] = i;
151  if (s->top_field_first) {
152  time_pp = s->pp_field_time - field_select + i;
153  time_pb = s->pb_field_time - field_select + i;
154  } else {
155  time_pp = s->pp_field_time + field_select - i;
156  time_pb = s->pb_field_time + field_select - i;
157  }
158  s->mv[0][i][0] = s->p_field_mv_table[i][0][mb_index][0] *
159  time_pb / time_pp + mx;
160  s->mv[0][i][1] = s->p_field_mv_table[i][0][mb_index][1] *
161  time_pb / time_pp + my;
162  s->mv[1][i][0] = mx ? s->mv[0][i][0] -
163  s->p_field_mv_table[i][0][mb_index][0]
164  : s->p_field_mv_table[i][0][mb_index][0] *
165  (time_pb - time_pp) / time_pp;
166  s->mv[1][i][1] = my ? s->mv[0][i][1] -
167  s->p_field_mv_table[i][0][mb_index][1]
168  : s->p_field_mv_table[i][0][mb_index][1] *
169  (time_pb - time_pp) / time_pp;
170  }
171  return MB_TYPE_DIRECT2 | MB_TYPE_16x8 |
173  } else {
174  ff_mpeg4_set_one_direct_mv(s, mx, my, 0);
175  s->mv[0][1][0] =
176  s->mv[0][2][0] =
177  s->mv[0][3][0] = s->mv[0][0][0];
178  s->mv[0][1][1] =
179  s->mv[0][2][1] =
180  s->mv[0][3][1] = s->mv[0][0][1];
181  s->mv[1][1][0] =
182  s->mv[1][2][0] =
183  s->mv[1][3][0] = s->mv[1][0][0];
184  s->mv[1][1][1] =
185  s->mv[1][2][1] =
186  s->mv[1][3][1] = s->mv[1][0][1];
187  if ((s->avctx->workaround_bugs & FF_BUG_DIRECT_BLOCKSIZE) ||
188  !s->quarter_sample)
189  s->mv_type = MV_TYPE_16X16;
190  else
191  s->mv_type = MV_TYPE_8X8;
192  // Note see prev line
194  }
195 }
IS_8X8
#define IS_8X8(a)
Definition: mpegutils.h:82
MV_TYPE_16X16
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:261
thread.h
MB_TYPE_16x8
#define MB_TYPE_16x8
Definition: mpegutils.h:48
MAX_RUN
#define MAX_RUN
Definition: rl.h:35
MB_TYPE_16x16
#define MB_TYPE_16x16
Definition: mpegutils.h:47
mpegvideo.h
tab_size
#define tab_size
Definition: mpeg4video.c:79
mpegutils.h
ff_mpeg4_init_rl_intra
av_cold void ff_mpeg4_init_rl_intra(void)
Definition: mpeg4video.c:36
ff_mpeg4_get_video_packet_prefix_length
int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s)
Definition: mpeg4video.c:42
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
av_cold
#define av_cold
Definition: attributes.h:90
ff_mpeg4_set_direct_mv
int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my)
Definition: mpeg4video.c:129
tab_bias
#define tab_bias
Definition: mpeg4video.c:80
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_mpeg4_rl_intra
RLTable ff_mpeg4_rl_intra
Definition: mpeg4data.h:108
ff_mpeg4_set_one_direct_mv
static void ff_mpeg4_set_one_direct_mv(MpegEncContext *s, int mx, int my, int i)
Definition: mpeg4video.c:93
FF_BUG_DIRECT_BLOCKSIZE
#define FF_BUG_DIRECT_BLOCKSIZE
Definition: avcodec.h:1358
ff_rl_init
av_cold void ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Initialize index_run, max_level and max_run from n, last, table_vlc, table_run and table_level.
Definition: rl.c:44
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
MB_TYPE_8x8
#define MB_TYPE_8x8
Definition: mpegutils.h:50
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
IS_INTERLACED
#define IS_INTERLACED(a)
Definition: mpegutils.h:76
AVOnce
#define AVOnce
Definition: thread.h:202
MV_TYPE_8X8
#define MV_TYPE_8X8
4 vectors (H.263, MPEG-4 4MV)
Definition: mpegvideo.h:262
MAX_LEVEL
#define MAX_LEVEL
Definition: rl.h:36
ff_mpeg4_clean_buffers
void ff_mpeg4_clean_buffers(MpegEncContext *s)
Definition: mpeg4video.c:57
MB_TYPE_INTERLACED
#define MB_TYPE_INTERLACED
Definition: mpegutils.h:51
MV_TYPE_FIELD
#define MV_TYPE_FIELD
2 vectors, one per field
Definition: mpegvideo.h:264
mpeg4_init_rl_intra
static av_cold void mpeg4_init_rl_intra(void)
Definition: mpeg4video.c:30
MB_TYPE_L0L1
#define MB_TYPE_L0L1
Definition: mpegutils.h:62
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
ff_mpeg4_init_direct_mv
void ff_mpeg4_init_direct_mv(MpegEncContext *s)
Definition: mpeg4video.c:83
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:281
mpeg4video.h
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
FFMAX3
#define FFMAX3(a, b, c)
Definition: macros.h:48
MB_TYPE_DIRECT2
#define MB_TYPE_DIRECT2
Definition: mpegutils.h:52
AV_PICTURE_TYPE_S
@ AV_PICTURE_TYPE_S
S(GMC)-VOP MPEG-4.
Definition: avutil.h:282
mpeg4data.h
MpegEncContext
MpegEncContext.
Definition: mpegvideo.h:67