FFmpeg
dv.c
Go to the documentation of this file.
1 /*
2  * DV decoder
3  * Copyright (c) 2002 Fabrice Bellard
4  * Copyright (c) 2004 Roman Shaposhnik
5  *
6  * DV encoder
7  * Copyright (c) 2003 Roman Shaposhnik
8  *
9  * 50 Mbps (DVCPRO50) support
10  * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
11  *
12  * 100 Mbps (DVCPRO HD) support
13  * Initial code by Daniel Maas <dmaas@maasdigital.com> (funded by BBC R&D)
14  * Final code by Roman Shaposhnik
15  *
16  * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
17  * of DV technical info.
18  *
19  * This file is part of FFmpeg.
20  *
21  * FFmpeg is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU Lesser General Public
23  * License as published by the Free Software Foundation; either
24  * version 2.1 of the License, or (at your option) any later version.
25  *
26  * FFmpeg is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29  * Lesser General Public License for more details.
30  *
31  * You should have received a copy of the GNU Lesser General Public
32  * License along with FFmpeg; if not, write to the Free Software
33  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34  */
35 
36 /**
37  * @file
38  * DV codec.
39  */
40 
41 #include "libavutil/internal.h"
42 #include "libavutil/pixdesc.h"
43 
44 #include "avcodec.h"
45 #include "dv.h"
46 #include "dvdata.h"
47 #include "internal.h"
48 #include "put_bits.h"
49 #include "simple_idct.h"
50 
51 static inline void dv_calc_mb_coordinates(const AVDVProfile *d, int chan,
52  int seq, int slot, uint16_t *tbl)
53 {
54  static const uint8_t off[] = { 2, 6, 8, 0, 4 };
55  static const uint8_t shuf1[] = { 36, 18, 54, 0, 72 };
56  static const uint8_t shuf2[] = { 24, 12, 36, 0, 48 };
57  static const uint8_t shuf3[] = { 18, 9, 27, 0, 36 };
58 
59  static const uint8_t l_start[] = { 0, 4, 9, 13, 18, 22, 27, 31, 36, 40 };
60  static const uint8_t l_start_shuffled[] = { 9, 4, 13, 0, 18 };
61 
62  static const uint8_t serpent1[] = {
63  0, 1, 2, 2, 1, 0,
64  0, 1, 2, 2, 1, 0,
65  0, 1, 2, 2, 1, 0,
66  0, 1, 2, 2, 1, 0,
67  0, 1, 2
68  };
69  static const uint8_t serpent2[] = {
70  0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0,
71  0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0,
72  0, 1, 2, 3, 4, 5
73  };
74 
75  static const uint8_t remap[][2] = {
76  { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, /* dummy */
77  { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 }, { 10, 0 },
78  { 10, 1 }, { 10, 2 }, { 10, 3 }, { 20, 0 }, { 20, 1 },
79  { 20, 2 }, { 20, 3 }, { 30, 0 }, { 30, 1 }, { 30, 2 },
80  { 30, 3 }, { 40, 0 }, { 40, 1 }, { 40, 2 }, { 40, 3 },
81  { 50, 0 }, { 50, 1 }, { 50, 2 }, { 50, 3 }, { 60, 0 },
82  { 60, 1 }, { 60, 2 }, { 60, 3 }, { 70, 0 }, { 70, 1 },
83  { 70, 2 }, { 70, 3 }, { 0, 64 }, { 0, 65 }, { 0, 66 },
84  { 10, 64 }, { 10, 65 }, { 10, 66 }, { 20, 64 }, { 20, 65 },
85  { 20, 66 }, { 30, 64 }, { 30, 65 }, { 30, 66 }, { 40, 64 },
86  { 40, 65 }, { 40, 66 }, { 50, 64 }, { 50, 65 }, { 50, 66 },
87  { 60, 64 }, { 60, 65 }, { 60, 66 }, { 70, 64 }, { 70, 65 },
88  { 70, 66 }, { 0, 67 }, { 20, 67 }, { 40, 67 }, { 60, 67 }
89  };
90 
91  int i, k, m;
92  int x, y, blk;
93 
94  for (m = 0; m < 5; m++) {
95  switch (d->width) {
96  case 1440:
97  blk = (chan * 11 + seq) * 27 + slot;
98 
99  if (chan == 0 && seq == 11) {
100  x = m * 27 + slot;
101  if (x < 90) {
102  y = 0;
103  } else {
104  x = (x - 90) * 2;
105  y = 67;
106  }
107  } else {
108  i = (4 * chan + blk + off[m]) % 11;
109  k = (blk / 11) % 27;
110 
111  x = shuf1[m] + (chan & 1) * 9 + k % 9;
112  y = (i * 3 + k / 9) * 2 + (chan >> 1) + 1;
113  }
114  tbl[m] = (x << 1) | (y << 9);
115  break;
116  case 1280:
117  blk = (chan * 10 + seq) * 27 + slot;
118 
119  i = (4 * chan + (seq / 5) + 2 * blk + off[m]) % 10;
120  k = (blk / 5) % 27;
121 
122  x = shuf1[m] + (chan & 1) * 9 + k % 9;
123  y = (i * 3 + k / 9) * 2 + (chan >> 1) + 4;
124 
125  if (x >= 80) {
126  x = remap[y][0] + ((x - 80) << (y > 59));
127  y = remap[y][1];
128  }
129  tbl[m] = (x << 1) | (y << 9);
130  break;
131  case 960:
132  blk = (chan * 10 + seq) * 27 + slot;
133 
134  i = (4 * chan + (seq / 5) + 2 * blk + off[m]) % 10;
135  k = (blk / 5) % 27 + (i & 1) * 3;
136 
137  x = shuf2[m] + k % 6 + 6 * (chan & 1);
138  y = l_start[i] + k / 6 + 45 * (chan >> 1);
139  tbl[m] = (x << 1) | (y << 9);
140  break;
141  case 720:
142  switch (d->pix_fmt) {
143  case AV_PIX_FMT_YUV422P:
144  x = shuf3[m] + slot / 3;
145  y = serpent1[slot] +
146  ((((seq + off[m]) % d->difseg_size) << 1) + chan) * 3;
147  tbl[m] = (x << 1) | (y << 8);
148  break;
149  case AV_PIX_FMT_YUV420P:
150  x = shuf3[m] + slot / 3;
151  y = serpent1[slot] +
152  ((seq + off[m]) % d->difseg_size) * 3;
153  tbl[m] = (x << 1) | (y << 9);
154  break;
155  case AV_PIX_FMT_YUV411P:
156  i = (seq + off[m]) % d->difseg_size;
157  k = slot + ((m == 1 || m == 2) ? 3 : 0);
158 
159  x = l_start_shuffled[m] + k / 6;
160  y = serpent2[k] + i * 6;
161  if (x > 21)
162  y = y * 2 - i * 6;
163  tbl[m] = (x << 2) | (y << 8);
164  break;
165  }
166  default:
167  break;
168  }
169  }
170 }
171 
173 {
174  int j, i, c, s, p;
175 
176  p = i = 0;
177  for (c = 0; c < d->n_difchan; c++) {
178  for (s = 0; s < d->difseg_size; s++) {
179  p += 6;
180  for (j = 0; j < 27; j++) {
181  p += !(j % 3);
182  if (!(DV_PROFILE_IS_1080i50(d) && c != 0 && s == 11) &&
183  !(DV_PROFILE_IS_720p50(d) && s > 9)) {
184  dv_calc_mb_coordinates(d, c, s, j, &ctx->work_chunks[i].mb_coordinates[0]);
185  ctx->work_chunks[i++].buf_offset = p;
186  }
187  p += 5;
188  }
189  }
190  }
191 
192  return 0;
193 }
194 
196 {
197  DVVideoContext *s = avctx->priv_data;
198 
199  s->avctx = avctx;
201 
202  return 0;
203 }
pixdesc.h
internal.h
dv_calc_mb_coordinates
static void dv_calc_mb_coordinates(const AVDVProfile *d, int chan, int seq, int slot, uint16_t *tbl)
Definition: dv.c:51
DV_PROFILE_IS_1080i50
#define DV_PROFILE_IS_1080i50(p)
Definition: dv.h:85
ff_dv_init_dynamic_tables
int ff_dv_init_dynamic_tables(DVVideoContext *ctx, const AVDVProfile *d)
Definition: dv.c:172
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:257
ctx
AVFormatContext * ctx
Definition: movenc.c:48
simple_idct.h
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
blk
#define blk(i)
Definition: sha.c:185
DV_PROFILE_IS_720p50
#define DV_PROFILE_IS_720p50(p)
Definition: dv.h:87
AVCHROMA_LOC_TOPLEFT
@ AVCHROMA_LOC_TOPLEFT
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:599
DVVideoContext
Definition: dv.h:41
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
dv.h
remap
static const int remap[16]
Definition: msvideo1enc.c:64
i
int i
Definition: input.c:406
internal.h
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:970
avcodec.h
AVCodecContext
main external API structure.
Definition: avcodec.h:379
AVDVProfile
Definition: dv_profile.h:38
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
ff_dvvideo_init
av_cold int ff_dvvideo_init(AVCodecContext *avctx)
Definition: dv.c:195
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:406
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
d
d
Definition: ffmpeg_filter.c:156
put_bits.h
dvdata.h