00001 /* 00002 * DV decoder 00003 * Copyright (c) 2002 Fabrice Bellard 00004 * Copyright (c) 2004 Roman Shaposhnik 00005 * 00006 * DV encoder 00007 * Copyright (c) 2003 Roman Shaposhnik 00008 * 00009 * 50 Mbps (DVCPRO50) support 00010 * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com> 00011 * 00012 * 100 Mbps (DVCPRO HD) support 00013 * Initial code by Daniel Maas <dmaas@maasdigital.com> (funded by BBC R&D) 00014 * Final code by Roman Shaposhnik 00015 * 00016 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth 00017 * of DV technical info. 00018 * 00019 * This file is part of FFmpeg. 00020 * 00021 * FFmpeg is free software; you can redistribute it and/or 00022 * modify it under the terms of the GNU Lesser General Public 00023 * License as published by the Free Software Foundation; either 00024 * version 2.1 of the License, or (at your option) any later version. 00025 * 00026 * FFmpeg is distributed in the hope that it will be useful, 00027 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00028 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00029 * Lesser General Public License for more details. 00030 * 00031 * You should have received a copy of the GNU Lesser General Public 00032 * License along with FFmpeg; if not, write to the Free Software 00033 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00034 */ 00035 00040 #define ALT_BITSTREAM_READER 00041 #include "avcodec.h" 00042 #include "dsputil.h" 00043 #include "bitstream.h" 00044 #include "simple_idct.h" 00045 #include "dvdata.h" 00046 00047 //#undef NDEBUG 00048 //#include <assert.h> 00049 00050 typedef struct DVVideoContext { 00051 const DVprofile *sys; 00052 AVFrame picture; 00053 AVCodecContext *avctx; 00054 uint8_t *buf; 00055 00056 uint8_t dv_zigzag[2][64]; 00057 00058 void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size); 00059 void (*fdct[2])(DCTELEM *block); 00060 void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block); 00061 me_cmp_func ildct_cmp; 00062 } DVVideoContext; 00063 00064 #define TEX_VLC_BITS 9 00065 00066 #if CONFIG_SMALL 00067 #define DV_VLC_MAP_RUN_SIZE 15 00068 #define DV_VLC_MAP_LEV_SIZE 23 00069 #else 00070 #define DV_VLC_MAP_RUN_SIZE 64 00071 #define DV_VLC_MAP_LEV_SIZE 512 //FIXME sign was removed so this should be /2 but needs check 00072 #endif 00073 00074 /* XXX: also include quantization */ 00075 static RL_VLC_ELEM dv_rl_vlc[1184]; 00076 /* VLC encoding lookup table */ 00077 static struct dv_vlc_pair { 00078 uint32_t vlc; 00079 uint8_t size; 00080 } dv_vlc_map[DV_VLC_MAP_RUN_SIZE][DV_VLC_MAP_LEV_SIZE]; 00081 00082 static inline int dv_work_pool_size(const DVprofile *d) 00083 { 00084 int size = d->n_difchan*d->difseg_size*27; 00085 if (DV_PROFILE_IS_1080i50(d)) 00086 size -= 3*27; 00087 if (DV_PROFILE_IS_720p50(d)) 00088 size -= 4*27; 00089 return size; 00090 } 00091 00092 static inline void dv_calc_mb_coordinates(const DVprofile *d, int chan, int seq, int slot, 00093 uint16_t *tbl) 00094 { 00095 static const uint8_t off[] = { 2, 6, 8, 0, 4 }; 00096 static const uint8_t shuf1[] = { 36, 18, 54, 0, 72 }; 00097 static const uint8_t shuf2[] = { 24, 12, 36, 0, 48 }; 00098 static const uint8_t shuf3[] = { 18, 9, 27, 0, 36 }; 00099 00100 static const uint8_t l_start[] = {0, 4, 9, 13, 18, 22, 27, 31, 36, 40}; 00101 static const uint8_t l_start_shuffled[] = { 9, 4, 13, 0, 18 }; 00102 00103 static const uint8_t serpent1[] = {0, 1, 2, 2, 1, 0, 00104 0, 1, 2, 2, 1, 0, 00105 0, 1, 2, 2, 1, 0, 00106 0, 1, 2, 2, 1, 0, 00107 0, 1, 2}; 00108 static const uint8_t serpent2[] = {0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0, 00109 0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0, 00110 0, 1, 2, 3, 4, 5}; 00111 00112 static const uint8_t remap[][2] = {{ 0, 0}, { 0, 0}, { 0, 0}, { 0, 0}, /* dummy */ 00113 { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3}, {10, 0}, 00114 {10, 1}, {10, 2}, {10, 3}, {20, 0}, {20, 1}, 00115 {20, 2}, {20, 3}, {30, 0}, {30, 1}, {30, 2}, 00116 {30, 3}, {40, 0}, {40, 1}, {40, 2}, {40, 3}, 00117 {50, 0}, {50, 1}, {50, 2}, {50, 3}, {60, 0}, 00118 {60, 1}, {60, 2}, {60, 3}, {70, 0}, {70, 1}, 00119 {70, 2}, {70, 3}, { 0,64}, { 0,65}, { 0,66}, 00120 {10,64}, {10,65}, {10,66}, {20,64}, {20,65}, 00121 {20,66}, {30,64}, {30,65}, {30,66}, {40,64}, 00122 {40,65}, {40,66}, {50,64}, {50,65}, {50,66}, 00123 {60,64}, {60,65}, {60,66}, {70,64}, {70,65}, 00124 {70,66}, { 0,67}, {20,67}, {40,67}, {60,67}}; 00125 00126 int i, k, m; 00127 int x, y, blk; 00128 00129 for (m=0; m<5; m++) { 00130 switch (d->width) { 00131 case 1440: 00132 blk = (chan*11+seq)*27+slot; 00133 00134 if (chan == 0 && seq == 11) { 00135 x = m*27+slot; 00136 if (x<90) { 00137 y = 0; 00138 } else { 00139 x = (x - 90)*2; 00140 y = 67; 00141 } 00142 } else { 00143 i = (4*chan + blk + off[m])%11; 00144 k = (blk/11)%27; 00145 00146 x = shuf1[m] + (chan&1)*9 + k%9; 00147 y = (i*3+k/9)*2 + (chan>>1) + 1; 00148 } 00149 tbl[m] = (x<<1)|(y<<9); 00150 break; 00151 case 1280: 00152 blk = (chan*10+seq)*27+slot; 00153 00154 i = (4*chan + (seq/5) + 2*blk + off[m])%10; 00155 k = (blk/5)%27; 00156 00157 x = shuf1[m]+(chan&1)*9 + k%9; 00158 y = (i*3+k/9)*2 + (chan>>1) + 4; 00159 00160 if (x >= 80) { 00161 x = remap[y][0]+((x-80)<<(y>59)); 00162 y = remap[y][1]; 00163 } 00164 tbl[m] = (x<<1)|(y<<9); 00165 break; 00166 case 960: 00167 blk = (chan*10+seq)*27+slot; 00168 00169 i = (4*chan + (seq/5) + 2*blk + off[m])%10; 00170 k = (blk/5)%27 + (i&1)*3; 00171 00172 x = shuf2[m] + k%6 + 6*(chan&1); 00173 y = l_start[i] + k/6 + 45*(chan>>1); 00174 tbl[m] = (x<<1)|(y<<9); 00175 break; 00176 case 720: 00177 switch (d->pix_fmt) { 00178 case PIX_FMT_YUV422P: 00179 x = shuf3[m] + slot/3; 00180 y = serpent1[slot] + 00181 ((((seq + off[m]) % d->difseg_size)<<1) + chan)*3; 00182 tbl[m] = (x<<1)|(y<<8); 00183 break; 00184 case PIX_FMT_YUV420P: 00185 x = shuf3[m] + slot/3; 00186 y = serpent1[slot] + 00187 ((seq + off[m]) % d->difseg_size)*3; 00188 tbl[m] = (x<<1)|(y<<9); 00189 break; 00190 case PIX_FMT_YUV411P: 00191 i = (seq + off[m]) % d->difseg_size; 00192 k = slot + ((m==1||m==2)?3:0); 00193 00194 x = l_start_shuffled[m] + k/6; 00195 y = serpent2[k] + i*6; 00196 if (x>21) 00197 y = y*2 - i*6; 00198 tbl[m] = (x<<2)|(y<<8); 00199 break; 00200 } 00201 default: 00202 break; 00203 } 00204 } 00205 } 00206 00207 static int dv_init_dynamic_tables(const DVprofile *d) 00208 { 00209 int j,i,c,s,p; 00210 uint32_t *factor1, *factor2; 00211 const int *iweight1, *iweight2; 00212 00213 if (!d->work_chunks[dv_work_pool_size(d)-1].buf_offset) { 00214 p = i = 0; 00215 for (c=0; c<d->n_difchan; c++) { 00216 for (s=0; s<d->difseg_size; s++) { 00217 p += 6; 00218 for (j=0; j<27; j++) { 00219 p += !(j%3); 00220 if (!(DV_PROFILE_IS_1080i50(d) && c != 0 && s == 11) && 00221 !(DV_PROFILE_IS_720p50(d) && s > 9)) { 00222 dv_calc_mb_coordinates(d, c, s, j, &d->work_chunks[i].mb_coordinates[0]); 00223 d->work_chunks[i++].buf_offset = p; 00224 } 00225 p += 5; 00226 } 00227 } 00228 } 00229 } 00230 00231 if (!d->idct_factor[DV_PROFILE_IS_HD(d)?8191:5631]) { 00232 factor1 = &d->idct_factor[0]; 00233 factor2 = &d->idct_factor[DV_PROFILE_IS_HD(d)?4096:2816]; 00234 if (d->height == 720) { 00235 iweight1 = &dv_iweight_720_y[0]; 00236 iweight2 = &dv_iweight_720_c[0]; 00237 } else { 00238 iweight1 = &dv_iweight_1080_y[0]; 00239 iweight2 = &dv_iweight_1080_c[0]; 00240 } 00241 if (DV_PROFILE_IS_HD(d)) { 00242 for (c = 0; c < 4; c++) { 00243 for (s = 0; s < 16; s++) { 00244 for (i = 0; i < 64; i++) { 00245 *factor1++ = (dv100_qstep[s] << (c + 9)) * iweight1[i]; 00246 *factor2++ = (dv100_qstep[s] << (c + 9)) * iweight2[i]; 00247 } 00248 } 00249 } 00250 } else { 00251 iweight1 = &dv_iweight_88[0]; 00252 for (j = 0; j < 2; j++, iweight1 = &dv_iweight_248[0]) { 00253 for (s = 0; s < 22; s++) { 00254 for (i = c = 0; c < 4; c++) { 00255 for (; i < dv_quant_areas[c]; i++) { 00256 *factor1 = iweight1[i] << (dv_quant_shifts[s][c] + 1); 00257 *factor2++ = (*factor1++) << 1; 00258 } 00259 } 00260 } 00261 } 00262 } 00263 } 00264 00265 return 0; 00266 } 00267 00268 static av_cold int dvvideo_init(AVCodecContext *avctx) 00269 { 00270 DVVideoContext *s = avctx->priv_data; 00271 DSPContext dsp; 00272 static int done = 0; 00273 int i, j; 00274 00275 if (!done) { 00276 VLC dv_vlc; 00277 uint16_t new_dv_vlc_bits[NB_DV_VLC*2]; 00278 uint8_t new_dv_vlc_len[NB_DV_VLC*2]; 00279 uint8_t new_dv_vlc_run[NB_DV_VLC*2]; 00280 int16_t new_dv_vlc_level[NB_DV_VLC*2]; 00281 00282 done = 1; 00283 00284 /* it's faster to include sign bit in a generic VLC parsing scheme */ 00285 for (i = 0, j = 0; i < NB_DV_VLC; i++, j++) { 00286 new_dv_vlc_bits[j] = dv_vlc_bits[i]; 00287 new_dv_vlc_len[j] = dv_vlc_len[i]; 00288 new_dv_vlc_run[j] = dv_vlc_run[i]; 00289 new_dv_vlc_level[j] = dv_vlc_level[i]; 00290 00291 if (dv_vlc_level[i]) { 00292 new_dv_vlc_bits[j] <<= 1; 00293 new_dv_vlc_len[j]++; 00294 00295 j++; 00296 new_dv_vlc_bits[j] = (dv_vlc_bits[i] << 1) | 1; 00297 new_dv_vlc_len[j] = dv_vlc_len[i] + 1; 00298 new_dv_vlc_run[j] = dv_vlc_run[i]; 00299 new_dv_vlc_level[j] = -dv_vlc_level[i]; 00300 } 00301 } 00302 00303 /* NOTE: as a trick, we use the fact the no codes are unused 00304 to accelerate the parsing of partial codes */ 00305 init_vlc(&dv_vlc, TEX_VLC_BITS, j, 00306 new_dv_vlc_len, 1, 1, new_dv_vlc_bits, 2, 2, 0); 00307 assert(dv_vlc.table_size == 1184); 00308 00309 for (i = 0; i < dv_vlc.table_size; i++){ 00310 int code = dv_vlc.table[i][0]; 00311 int len = dv_vlc.table[i][1]; 00312 int level, run; 00313 00314 if (len < 0){ //more bits needed 00315 run = 0; 00316 level = code; 00317 } else { 00318 run = new_dv_vlc_run [code] + 1; 00319 level = new_dv_vlc_level[code]; 00320 } 00321 dv_rl_vlc[i].len = len; 00322 dv_rl_vlc[i].level = level; 00323 dv_rl_vlc[i].run = run; 00324 } 00325 free_vlc(&dv_vlc); 00326 00327 for (i = 0; i < NB_DV_VLC - 1; i++) { 00328 if (dv_vlc_run[i] >= DV_VLC_MAP_RUN_SIZE) 00329 continue; 00330 #if CONFIG_SMALL 00331 if (dv_vlc_level[i] >= DV_VLC_MAP_LEV_SIZE) 00332 continue; 00333 #endif 00334 00335 if (dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size != 0) 00336 continue; 00337 00338 dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].vlc = 00339 dv_vlc_bits[i] << (!!dv_vlc_level[i]); 00340 dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size = 00341 dv_vlc_len[i] + (!!dv_vlc_level[i]); 00342 } 00343 for (i = 0; i < DV_VLC_MAP_RUN_SIZE; i++) { 00344 #if CONFIG_SMALL 00345 for (j = 1; j < DV_VLC_MAP_LEV_SIZE; j++) { 00346 if (dv_vlc_map[i][j].size == 0) { 00347 dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc | 00348 (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size)); 00349 dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size + 00350 dv_vlc_map[0][j].size; 00351 } 00352 } 00353 #else 00354 for (j = 1; j < DV_VLC_MAP_LEV_SIZE/2; j++) { 00355 if (dv_vlc_map[i][j].size == 0) { 00356 dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc | 00357 (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size)); 00358 dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size + 00359 dv_vlc_map[0][j].size; 00360 } 00361 dv_vlc_map[i][((uint16_t)(-j))&0x1ff].vlc = 00362 dv_vlc_map[i][j].vlc | 1; 00363 dv_vlc_map[i][((uint16_t)(-j))&0x1ff].size = 00364 dv_vlc_map[i][j].size; 00365 } 00366 #endif 00367 } 00368 } 00369 00370 /* Generic DSP setup */ 00371 dsputil_init(&dsp, avctx); 00372 ff_set_cmp(&dsp, dsp.ildct_cmp, avctx->ildct_cmp); 00373 s->get_pixels = dsp.get_pixels; 00374 s->ildct_cmp = dsp.ildct_cmp[5]; 00375 00376 /* 88DCT setup */ 00377 s->fdct[0] = dsp.fdct; 00378 s->idct_put[0] = dsp.idct_put; 00379 for (i = 0; i < 64; i++) 00380 s->dv_zigzag[0][i] = dsp.idct_permutation[ff_zigzag_direct[i]]; 00381 00382 /* 248DCT setup */ 00383 s->fdct[1] = dsp.fdct248; 00384 s->idct_put[1] = ff_simple_idct248_put; // FIXME: need to add it to DSP 00385 if (avctx->lowres){ 00386 for (i = 0; i < 64; i++){ 00387 int j = ff_zigzag248_direct[i]; 00388 s->dv_zigzag[1][i] = dsp.idct_permutation[(j & 7) + (j & 8) * 4 + (j & 48) / 2]; 00389 } 00390 }else 00391 memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64); 00392 00393 avctx->coded_frame = &s->picture; 00394 s->avctx = avctx; 00395 00396 return 0; 00397 } 00398 00399 // #define VLC_DEBUG 00400 // #define printf(...) av_log(NULL, AV_LOG_ERROR, __VA_ARGS__) 00401 00402 typedef struct BlockInfo { 00403 const uint32_t *factor_table; 00404 const uint8_t *scan_table; 00405 uint8_t pos; /* position in block */ 00406 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block); 00407 uint8_t partial_bit_count; 00408 uint16_t partial_bit_buffer; 00409 int shift_offset; 00410 } BlockInfo; 00411 00412 /* bit budget for AC only in 5 MBs */ 00413 static const int vs_total_ac_bits = (100 * 4 + 68*2) * 5; 00414 /* see dv_88_areas and dv_248_areas for details */ 00415 static const int mb_area_start[5] = { 1, 6, 21, 43, 64 }; 00416 00417 static inline int put_bits_left(PutBitContext* s) 00418 { 00419 return (s->buf_end - s->buf) * 8 - put_bits_count(s); 00420 } 00421 00422 /* decode ac coefficients */ 00423 static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block) 00424 { 00425 int last_index = gb->size_in_bits; 00426 const uint8_t *scan_table = mb->scan_table; 00427 const uint32_t *factor_table = mb->factor_table; 00428 int pos = mb->pos; 00429 int partial_bit_count = mb->partial_bit_count; 00430 int level, run, vlc_len, index; 00431 00432 OPEN_READER(re, gb); 00433 UPDATE_CACHE(re, gb); 00434 00435 /* if we must parse a partial vlc, we do it here */ 00436 if (partial_bit_count > 0) { 00437 re_cache = ((unsigned)re_cache >> partial_bit_count) | 00438 (mb->partial_bit_buffer << (sizeof(re_cache) * 8 - partial_bit_count)); 00439 re_index -= partial_bit_count; 00440 mb->partial_bit_count = 0; 00441 } 00442 00443 /* get the AC coefficients until last_index is reached */ 00444 for (;;) { 00445 #ifdef VLC_DEBUG 00446 printf("%2d: bits=%04x index=%d\n", pos, SHOW_UBITS(re, gb, 16), re_index); 00447 #endif 00448 /* our own optimized GET_RL_VLC */ 00449 index = NEG_USR32(re_cache, TEX_VLC_BITS); 00450 vlc_len = dv_rl_vlc[index].len; 00451 if (vlc_len < 0) { 00452 index = NEG_USR32((unsigned)re_cache << TEX_VLC_BITS, -vlc_len) + dv_rl_vlc[index].level; 00453 vlc_len = TEX_VLC_BITS - vlc_len; 00454 } 00455 level = dv_rl_vlc[index].level; 00456 run = dv_rl_vlc[index].run; 00457 00458 /* gotta check if we're still within gb boundaries */ 00459 if (re_index + vlc_len > last_index) { 00460 /* should be < 16 bits otherwise a codeword could have been parsed */ 00461 mb->partial_bit_count = last_index - re_index; 00462 mb->partial_bit_buffer = NEG_USR32(re_cache, mb->partial_bit_count); 00463 re_index = last_index; 00464 break; 00465 } 00466 re_index += vlc_len; 00467 00468 #ifdef VLC_DEBUG 00469 printf("run=%d level=%d\n", run, level); 00470 #endif 00471 pos += run; 00472 if (pos >= 64) 00473 break; 00474 00475 level = (level * factor_table[pos] + (1 << (dv_iweight_bits - 1))) >> dv_iweight_bits; 00476 block[scan_table[pos]] = level; 00477 00478 UPDATE_CACHE(re, gb); 00479 } 00480 CLOSE_READER(re, gb); 00481 mb->pos = pos; 00482 } 00483 00484 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb) 00485 { 00486 int bits_left = get_bits_left(gb); 00487 while (bits_left >= MIN_CACHE_BITS) { 00488 put_bits(pb, MIN_CACHE_BITS, get_bits(gb, MIN_CACHE_BITS)); 00489 bits_left -= MIN_CACHE_BITS; 00490 } 00491 if (bits_left > 0) { 00492 put_bits(pb, bits_left, get_bits(gb, bits_left)); 00493 } 00494 } 00495 00496 static inline void dv_calculate_mb_xy(DVVideoContext *s, DVwork_chunk *work_chunk, int m, int *mb_x, int *mb_y) 00497 { 00498 *mb_x = work_chunk->mb_coordinates[m] & 0xff; 00499 *mb_y = work_chunk->mb_coordinates[m] >> 8; 00500 00501 /* We work with 720p frames split in half. The odd half-frame (chan==2,3) is displaced :-( */ 00502 if (s->sys->height == 720 && !(s->buf[1]&0x0C)) { 00503 *mb_y -= (*mb_y>17)?18:-72; /* shifting the Y coordinate down by 72/2 macro blocks */ 00504 } 00505 } 00506 00507 /* mb_x and mb_y are in units of 8 pixels */ 00508 static int dv_decode_video_segment(AVCodecContext *avctx, void *arg) 00509 { 00510 DVVideoContext *s = avctx->priv_data; 00511 DVwork_chunk *work_chunk = arg; 00512 int quant, dc, dct_mode, class1, j; 00513 int mb_index, mb_x, mb_y, last_index; 00514 int y_stride, linesize; 00515 DCTELEM *block, *block1; 00516 int c_offset; 00517 uint8_t *y_ptr; 00518 const uint8_t *buf_ptr; 00519 PutBitContext pb, vs_pb; 00520 GetBitContext gb; 00521 BlockInfo mb_data[5 * DV_MAX_BPM], *mb, *mb1; 00522 DECLARE_ALIGNED_16(DCTELEM, sblock[5*DV_MAX_BPM][64]); 00523 DECLARE_ALIGNED_8(uint8_t, mb_bit_buffer[80 + 4]); /* allow some slack */ 00524 DECLARE_ALIGNED_8(uint8_t, vs_bit_buffer[5 * 80 + 4]); /* allow some slack */ 00525 const int log2_blocksize = 3-s->avctx->lowres; 00526 int is_field_mode[5]; 00527 00528 assert((((int)mb_bit_buffer) & 7) == 0); 00529 assert((((int)vs_bit_buffer) & 7) == 0); 00530 00531 memset(sblock, 0, sizeof(sblock)); 00532 00533 /* pass 1 : read DC and AC coefficients in blocks */ 00534 buf_ptr = &s->buf[work_chunk->buf_offset*80]; 00535 block1 = &sblock[0][0]; 00536 mb1 = mb_data; 00537 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80); 00538 for (mb_index = 0; mb_index < 5; mb_index++, mb1 += s->sys->bpm, block1 += s->sys->bpm * 64) { 00539 /* skip header */ 00540 quant = buf_ptr[3] & 0x0f; 00541 buf_ptr += 4; 00542 init_put_bits(&pb, mb_bit_buffer, 80); 00543 mb = mb1; 00544 block = block1; 00545 is_field_mode[mb_index] = 0; 00546 for (j = 0; j < s->sys->bpm; j++) { 00547 last_index = s->sys->block_sizes[j]; 00548 init_get_bits(&gb, buf_ptr, last_index); 00549 00550 /* get the dc */ 00551 dc = get_sbits(&gb, 9); 00552 dct_mode = get_bits1(&gb); 00553 class1 = get_bits(&gb, 2); 00554 if (DV_PROFILE_IS_HD(s->sys)) { 00555 mb->idct_put = s->idct_put[0]; 00556 mb->scan_table = s->dv_zigzag[0]; 00557 mb->factor_table = &s->sys->idct_factor[(j >= 4)*4*16*64 + class1*16*64 + quant*64]; 00558 is_field_mode[mb_index] |= !j && dct_mode; 00559 } else { 00560 mb->idct_put = s->idct_put[dct_mode && log2_blocksize == 3]; 00561 mb->scan_table = s->dv_zigzag[dct_mode]; 00562 mb->factor_table = &s->sys->idct_factor[(class1 == 3)*2*22*64 + dct_mode*22*64 + 00563 (quant + dv_quant_offset[class1])*64]; 00564 } 00565 dc = dc << 2; 00566 /* convert to unsigned because 128 is not added in the 00567 standard IDCT */ 00568 dc += 1024; 00569 block[0] = dc; 00570 buf_ptr += last_index >> 3; 00571 mb->pos = 0; 00572 mb->partial_bit_count = 0; 00573 00574 #ifdef VLC_DEBUG 00575 printf("MB block: %d, %d ", mb_index, j); 00576 #endif 00577 dv_decode_ac(&gb, mb, block); 00578 00579 /* write the remaining bits in a new buffer only if the 00580 block is finished */ 00581 if (mb->pos >= 64) 00582 bit_copy(&pb, &gb); 00583 00584 block += 64; 00585 mb++; 00586 } 00587 00588 /* pass 2 : we can do it just after */ 00589 #ifdef VLC_DEBUG 00590 printf("***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index); 00591 #endif 00592 block = block1; 00593 mb = mb1; 00594 init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb)); 00595 flush_put_bits(&pb); 00596 for (j = 0; j < s->sys->bpm; j++, block += 64, mb++) { 00597 if (mb->pos < 64 && get_bits_left(&gb) > 0) { 00598 dv_decode_ac(&gb, mb, block); 00599 /* if still not finished, no need to parse other blocks */ 00600 if (mb->pos < 64) 00601 break; 00602 } 00603 } 00604 /* all blocks are finished, so the extra bytes can be used at 00605 the video segment level */ 00606 if (j >= s->sys->bpm) 00607 bit_copy(&vs_pb, &gb); 00608 } 00609 00610 /* we need a pass other the whole video segment */ 00611 #ifdef VLC_DEBUG 00612 printf("***pass 3 size=%d\n", put_bits_count(&vs_pb)); 00613 #endif 00614 block = &sblock[0][0]; 00615 mb = mb_data; 00616 init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb)); 00617 flush_put_bits(&vs_pb); 00618 for (mb_index = 0; mb_index < 5; mb_index++) { 00619 for (j = 0; j < s->sys->bpm; j++) { 00620 if (mb->pos < 64) { 00621 #ifdef VLC_DEBUG 00622 printf("start %d:%d\n", mb_index, j); 00623 #endif 00624 dv_decode_ac(&gb, mb, block); 00625 } 00626 if (mb->pos >= 64 && mb->pos < 127) 00627 av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos); 00628 block += 64; 00629 mb++; 00630 } 00631 } 00632 00633 /* compute idct and place blocks */ 00634 block = &sblock[0][0]; 00635 mb = mb_data; 00636 for (mb_index = 0; mb_index < 5; mb_index++) { 00637 dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y); 00638 00639 /* idct_put'ting luminance */ 00640 if ((s->sys->pix_fmt == PIX_FMT_YUV420P) || 00641 (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) || 00642 (s->sys->height >= 720 && mb_y != 134)) { 00643 y_stride = (s->picture.linesize[0] << ((!is_field_mode[mb_index]) * log2_blocksize)); 00644 } else { 00645 y_stride = (2 << log2_blocksize); 00646 } 00647 y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x) << log2_blocksize); 00648 linesize = s->picture.linesize[0] << is_field_mode[mb_index]; 00649 mb[0] .idct_put(y_ptr , linesize, block + 0*64); 00650 if (s->sys->video_stype == 4) { /* SD 422 */ 00651 mb[2].idct_put(y_ptr + (1 << log2_blocksize) , linesize, block + 2*64); 00652 } else { 00653 mb[1].idct_put(y_ptr + (1 << log2_blocksize) , linesize, block + 1*64); 00654 mb[2].idct_put(y_ptr + y_stride, linesize, block + 2*64); 00655 mb[3].idct_put(y_ptr + (1 << log2_blocksize) + y_stride, linesize, block + 3*64); 00656 } 00657 mb += 4; 00658 block += 4*64; 00659 00660 /* idct_put'ting chrominance */ 00661 c_offset = (((mb_y >> (s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] + 00662 (mb_x >> ((s->sys->pix_fmt == PIX_FMT_YUV411P) ? 2 : 1))) << log2_blocksize); 00663 for (j = 2; j; j--) { 00664 uint8_t *c_ptr = s->picture.data[j] + c_offset; 00665 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) { 00666 uint64_t aligned_pixels[64/8]; 00667 uint8_t *pixels = (uint8_t*)aligned_pixels; 00668 uint8_t *c_ptr1, *ptr1; 00669 int x, y; 00670 mb->idct_put(pixels, 8, block); 00671 for (y = 0; y < (1 << log2_blocksize); y++, c_ptr += s->picture.linesize[j], pixels += 8) { 00672 ptr1 = pixels + (1 << (log2_blocksize - 1)); 00673 c_ptr1 = c_ptr + (s->picture.linesize[j] << log2_blocksize); 00674 for (x = 0; x < (1 << (log2_blocksize - 1)); x++) { 00675 c_ptr[x] = pixels[x]; 00676 c_ptr1[x] = ptr1[x]; 00677 } 00678 } 00679 block += 64; mb++; 00680 } else { 00681 y_stride = (mb_y == 134) ? (1 << log2_blocksize) : 00682 s->picture.linesize[j] << ((!is_field_mode[mb_index]) * log2_blocksize); 00683 linesize = s->picture.linesize[j] << is_field_mode[mb_index]; 00684 (mb++)-> idct_put(c_ptr , linesize, block); block += 64; 00685 if (s->sys->bpm == 8) { 00686 (mb++)->idct_put(c_ptr + y_stride, linesize, block); block += 64; 00687 } 00688 } 00689 } 00690 } 00691 return 0; 00692 } 00693 00694 #if CONFIG_SMALL 00695 /* Converts run and level (where level != 0) pair into vlc, returning bit size */ 00696 static av_always_inline int dv_rl2vlc(int run, int level, int sign, uint32_t* vlc) 00697 { 00698 int size; 00699 if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) { 00700 *vlc = dv_vlc_map[run][level].vlc | sign; 00701 size = dv_vlc_map[run][level].size; 00702 } 00703 else { 00704 if (level < DV_VLC_MAP_LEV_SIZE) { 00705 *vlc = dv_vlc_map[0][level].vlc | sign; 00706 size = dv_vlc_map[0][level].size; 00707 } else { 00708 *vlc = 0xfe00 | (level << 1) | sign; 00709 size = 16; 00710 } 00711 if (run) { 00712 *vlc |= ((run < 16) ? dv_vlc_map[run-1][0].vlc : 00713 (0x1f80 | (run - 1))) << size; 00714 size += (run < 16) ? dv_vlc_map[run-1][0].size : 13; 00715 } 00716 } 00717 00718 return size; 00719 } 00720 00721 static av_always_inline int dv_rl2vlc_size(int run, int level) 00722 { 00723 int size; 00724 00725 if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) { 00726 size = dv_vlc_map[run][level].size; 00727 } 00728 else { 00729 size = (level < DV_VLC_MAP_LEV_SIZE) ? dv_vlc_map[0][level].size : 16; 00730 if (run) { 00731 size += (run < 16) ? dv_vlc_map[run-1][0].size : 13; 00732 } 00733 } 00734 return size; 00735 } 00736 #else 00737 static av_always_inline int dv_rl2vlc(int run, int l, int sign, uint32_t* vlc) 00738 { 00739 *vlc = dv_vlc_map[run][l].vlc | sign; 00740 return dv_vlc_map[run][l].size; 00741 } 00742 00743 static av_always_inline int dv_rl2vlc_size(int run, int l) 00744 { 00745 return dv_vlc_map[run][l].size; 00746 } 00747 #endif 00748 00749 typedef struct EncBlockInfo { 00750 int area_q[4]; 00751 int bit_size[4]; 00752 int prev[5]; 00753 int cur_ac; 00754 int cno; 00755 int dct_mode; 00756 DCTELEM mb[64]; 00757 uint8_t next[64]; 00758 uint8_t sign[64]; 00759 uint8_t partial_bit_count; 00760 uint32_t partial_bit_buffer; /* we can't use uint16_t here */ 00761 } EncBlockInfo; 00762 00763 static av_always_inline PutBitContext* dv_encode_ac(EncBlockInfo* bi, 00764 PutBitContext* pb_pool, 00765 PutBitContext* pb_end) 00766 { 00767 int prev, bits_left; 00768 PutBitContext* pb = pb_pool; 00769 int size = bi->partial_bit_count; 00770 uint32_t vlc = bi->partial_bit_buffer; 00771 00772 bi->partial_bit_count = bi->partial_bit_buffer = 0; 00773 for (;;){ 00774 /* Find suitable storage space */ 00775 for (; size > (bits_left = put_bits_left(pb)); pb++) { 00776 if (bits_left) { 00777 size -= bits_left; 00778 put_bits(pb, bits_left, vlc >> size); 00779 vlc = vlc & ((1 << size) - 1); 00780 } 00781 if (pb + 1 >= pb_end) { 00782 bi->partial_bit_count = size; 00783 bi->partial_bit_buffer = vlc; 00784 return pb; 00785 } 00786 } 00787 00788 /* Store VLC */ 00789 put_bits(pb, size, vlc); 00790 00791 if (bi->cur_ac >= 64) 00792 break; 00793 00794 /* Construct the next VLC */ 00795 prev = bi->cur_ac; 00796 bi->cur_ac = bi->next[prev]; 00797 if (bi->cur_ac < 64){ 00798 size = dv_rl2vlc(bi->cur_ac - prev - 1, bi->mb[bi->cur_ac], bi->sign[bi->cur_ac], &vlc); 00799 } else { 00800 size = 4; vlc = 6; /* End Of Block stamp */ 00801 } 00802 } 00803 return pb; 00804 } 00805 00806 static av_always_inline int dv_guess_dct_mode(DVVideoContext *s, uint8_t *data, int linesize) { 00807 if (s->avctx->flags & CODEC_FLAG_INTERLACED_DCT) { 00808 int ps = s->ildct_cmp(NULL, data, NULL, linesize, 8) - 400; 00809 if (ps > 0) { 00810 int is = s->ildct_cmp(NULL, data , NULL, linesize<<1, 4) + 00811 s->ildct_cmp(NULL, data + linesize, NULL, linesize<<1, 4); 00812 return (ps > is); 00813 } 00814 } 00815 00816 return 0; 00817 } 00818 00819 static av_always_inline int dv_init_enc_block(EncBlockInfo* bi, uint8_t *data, int linesize, DVVideoContext *s, int bias) 00820 { 00821 const int *weight; 00822 const uint8_t* zigzag_scan; 00823 DECLARE_ALIGNED_16(DCTELEM, blk[64]); 00824 int i, area; 00825 /* We offer two different methods for class number assignment: the 00826 method suggested in SMPTE 314M Table 22, and an improved 00827 method. The SMPTE method is very conservative; it assigns class 00828 3 (i.e. severe quantization) to any block where the largest AC 00829 component is greater than 36. FFmpeg's DV encoder tracks AC bit 00830 consumption precisely, so there is no need to bias most blocks 00831 towards strongly lossy compression. Instead, we assign class 2 00832 to most blocks, and use class 3 only when strictly necessary 00833 (for blocks whose largest AC component exceeds 255). */ 00834 00835 #if 0 /* SMPTE spec method */ 00836 static const int classes[] = {12, 24, 36, 0xffff}; 00837 #else /* improved FFmpeg method */ 00838 static const int classes[] = {-1, -1, 255, 0xffff}; 00839 #endif 00840 int max = classes[0]; 00841 int prev = 0; 00842 00843 assert((((int)blk) & 15) == 0); 00844 00845 bi->area_q[0] = bi->area_q[1] = bi->area_q[2] = bi->area_q[3] = 0; 00846 bi->partial_bit_count = 0; 00847 bi->partial_bit_buffer = 0; 00848 bi->cur_ac = 0; 00849 if (data) { 00850 bi->dct_mode = dv_guess_dct_mode(s, data, linesize); 00851 s->get_pixels(blk, data, linesize); 00852 s->fdct[bi->dct_mode](blk); 00853 } else { 00854 /* We rely on the fact that encoding all zeros leads to an immediate EOB, 00855 which is precisely what the spec calls for in the "dummy" blocks. */ 00856 memset(blk, 0, sizeof(blk)); 00857 bi->dct_mode = 0; 00858 } 00859 bi->mb[0] = blk[0]; 00860 00861 zigzag_scan = bi->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct; 00862 weight = bi->dct_mode ? dv_weight_248 : dv_weight_88; 00863 00864 for (area = 0; area < 4; area++) { 00865 bi->prev[area] = prev; 00866 bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :) 00867 for (i = mb_area_start[area]; i < mb_area_start[area+1]; i++) { 00868 int level = blk[zigzag_scan[i]]; 00869 00870 if (level + 15 > 30U) { 00871 bi->sign[i] = (level >> 31) & 1; 00872 /* weigh it and and shift down into range, adding for rounding */ 00873 /* the extra division by a factor of 2^4 reverses the 8x expansion of the DCT 00874 AND the 2x doubling of the weights */ 00875 level = (FFABS(level) * weight[i] + (1 << (dv_weight_bits+3))) >> (dv_weight_bits+4); 00876 bi->mb[i] = level; 00877 if (level > max) 00878 max = level; 00879 bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, level); 00880 bi->next[prev]= i; 00881 prev = i; 00882 } 00883 } 00884 } 00885 bi->next[prev]= i; 00886 for (bi->cno = 0; max > classes[bi->cno]; bi->cno++); 00887 00888 bi->cno += bias; 00889 00890 if (bi->cno >= 3) { 00891 bi->cno = 3; 00892 prev = 0; 00893 i = bi->next[prev]; 00894 for (area = 0; area < 4; area++) { 00895 bi->prev[area] = prev; 00896 bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :) 00897 for (; i < mb_area_start[area+1]; i = bi->next[i]) { 00898 bi->mb[i] >>= 1; 00899 00900 if (bi->mb[i]) { 00901 bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, bi->mb[i]); 00902 bi->next[prev]= i; 00903 prev = i; 00904 } 00905 } 00906 } 00907 bi->next[prev]= i; 00908 } 00909 00910 return bi->bit_size[0] + bi->bit_size[1] + bi->bit_size[2] + bi->bit_size[3]; 00911 } 00912 00913 static inline void dv_guess_qnos(EncBlockInfo* blks, int* qnos) 00914 { 00915 int size[5]; 00916 int i, j, k, a, prev, a2; 00917 EncBlockInfo* b; 00918 00919 size[0] = size[1] = size[2] = size[3] = size[4] = 1 << 24; 00920 do { 00921 b = blks; 00922 for (i = 0; i < 5; i++) { 00923 if (!qnos[i]) 00924 continue; 00925 00926 qnos[i]--; 00927 size[i] = 0; 00928 for (j = 0; j < 6; j++, b++) { 00929 for (a = 0; a < 4; a++) { 00930 if (b->area_q[a] != dv_quant_shifts[qnos[i] + dv_quant_offset[b->cno]][a]) { 00931 b->bit_size[a] = 1; // 4 areas 4 bits for EOB :) 00932 b->area_q[a]++; 00933 prev = b->prev[a]; 00934 assert(b->next[prev] >= mb_area_start[a+1] || b->mb[prev]); 00935 for (k = b->next[prev] ; k < mb_area_start[a+1]; k = b->next[k]) { 00936 b->mb[k] >>= 1; 00937 if (b->mb[k]) { 00938 b->bit_size[a] += dv_rl2vlc_size(k - prev - 1, b->mb[k]); 00939 prev = k; 00940 } else { 00941 if (b->next[k] >= mb_area_start[a+1] && b->next[k]<64){ 00942 for (a2 = a + 1; b->next[k] >= mb_area_start[a2+1]; a2++) 00943 b->prev[a2] = prev; 00944 assert(a2 < 4); 00945 assert(b->mb[b->next[k]]); 00946 b->bit_size[a2] += dv_rl2vlc_size(b->next[k] - prev - 1, b->mb[b->next[k]]) 00947 -dv_rl2vlc_size(b->next[k] - k - 1, b->mb[b->next[k]]); 00948 assert(b->prev[a2] == k && (a2 + 1 >= 4 || b->prev[a2+1] != k)); 00949 b->prev[a2] = prev; 00950 } 00951 b->next[prev] = b->next[k]; 00952 } 00953 } 00954 b->prev[a+1]= prev; 00955 } 00956 size[i] += b->bit_size[a]; 00957 } 00958 } 00959 if (vs_total_ac_bits >= size[0] + size[1] + size[2] + size[3] + size[4]) 00960 return; 00961 } 00962 } while (qnos[0]|qnos[1]|qnos[2]|qnos[3]|qnos[4]); 00963 00964 00965 for (a = 2; a == 2 || vs_total_ac_bits < size[0]; a += a){ 00966 b = blks; 00967 size[0] = 5 * 6 * 4; //EOB 00968 for (j = 0; j < 6 *5; j++, b++) { 00969 prev = b->prev[0]; 00970 for (k = b->next[prev]; k < 64; k = b->next[k]) { 00971 if (b->mb[k] < a && b->mb[k] > -a){ 00972 b->next[prev] = b->next[k]; 00973 }else{ 00974 size[0] += dv_rl2vlc_size(k - prev - 1, b->mb[k]); 00975 prev = k; 00976 } 00977 } 00978 } 00979 } 00980 } 00981 00982 static int dv_encode_video_segment(AVCodecContext *avctx, void *arg) 00983 { 00984 DVVideoContext *s = avctx->priv_data; 00985 DVwork_chunk *work_chunk = arg; 00986 int mb_index, i, j; 00987 int mb_x, mb_y, c_offset, linesize, y_stride; 00988 uint8_t* y_ptr; 00989 uint8_t* dif; 00990 uint8_t scratch[64]; 00991 EncBlockInfo enc_blks[5*DV_MAX_BPM]; 00992 PutBitContext pbs[5*DV_MAX_BPM]; 00993 PutBitContext* pb; 00994 EncBlockInfo* enc_blk; 00995 int vs_bit_size = 0; 00996 int qnos[5] = {15, 15, 15, 15, 15}; /* No quantization */ 00997 int* qnosp = &qnos[0]; 00998 00999 dif = &s->buf[work_chunk->buf_offset*80]; 01000 enc_blk = &enc_blks[0]; 01001 for (mb_index = 0; mb_index < 5; mb_index++) { 01002 dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y); 01003 01004 /* initializing luminance blocks */ 01005 if ((s->sys->pix_fmt == PIX_FMT_YUV420P) || 01006 (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) || 01007 (s->sys->height >= 720 && mb_y != 134)) { 01008 y_stride = s->picture.linesize[0] << 3; 01009 } else { 01010 y_stride = 16; 01011 } 01012 y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x) << 3); 01013 linesize = s->picture.linesize[0]; 01014 01015 if (s->sys->video_stype == 4) { /* SD 422 */ 01016 vs_bit_size += 01017 dv_init_enc_block(enc_blk+0, y_ptr , linesize, s, 0) + 01018 dv_init_enc_block(enc_blk+1, NULL , linesize, s, 0) + 01019 dv_init_enc_block(enc_blk+2, y_ptr + 8 , linesize, s, 0) + 01020 dv_init_enc_block(enc_blk+3, NULL , linesize, s, 0); 01021 } else { 01022 vs_bit_size += 01023 dv_init_enc_block(enc_blk+0, y_ptr , linesize, s, 0) + 01024 dv_init_enc_block(enc_blk+1, y_ptr + 8 , linesize, s, 0) + 01025 dv_init_enc_block(enc_blk+2, y_ptr + y_stride, linesize, s, 0) + 01026 dv_init_enc_block(enc_blk+3, y_ptr + 8 + y_stride, linesize, s, 0); 01027 } 01028 enc_blk += 4; 01029 01030 /* initializing chrominance blocks */ 01031 c_offset = (((mb_y >> (s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] + 01032 (mb_x >> ((s->sys->pix_fmt == PIX_FMT_YUV411P) ? 2 : 1))) << 3); 01033 for (j = 2; j; j--) { 01034 uint8_t *c_ptr = s->picture.data[j] + c_offset; 01035 linesize = s->picture.linesize[j]; 01036 y_stride = (mb_y == 134) ? 8 : (s->picture.linesize[j] << 3); 01037 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) { 01038 uint8_t* d; 01039 uint8_t* b = scratch; 01040 for (i = 0; i < 8; i++) { 01041 d = c_ptr + (linesize << 3); 01042 b[0] = c_ptr[0]; b[1] = c_ptr[1]; b[2] = c_ptr[2]; b[3] = c_ptr[3]; 01043 b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3]; 01044 c_ptr += linesize; 01045 b += 8; 01046 } 01047 c_ptr = scratch; 01048 linesize = 8; 01049 } 01050 01051 vs_bit_size += dv_init_enc_block( enc_blk++, c_ptr , linesize, s, 1); 01052 if (s->sys->bpm == 8) { 01053 vs_bit_size += dv_init_enc_block(enc_blk++, c_ptr + y_stride, linesize, s, 1); 01054 } 01055 } 01056 } 01057 01058 if (vs_total_ac_bits < vs_bit_size) 01059 dv_guess_qnos(&enc_blks[0], qnosp); 01060 01061 /* DIF encoding process */ 01062 for (j=0; j<5*s->sys->bpm;) { 01063 int start_mb = j; 01064 01065 dif[3] = *qnosp++; 01066 dif += 4; 01067 01068 /* First pass over individual cells only */ 01069 for (i=0; i<s->sys->bpm; i++, j++) { 01070 int sz = s->sys->block_sizes[i]>>3; 01071 01072 init_put_bits(&pbs[j], dif, sz); 01073 put_bits(&pbs[j], 9, (uint16_t)(((enc_blks[j].mb[0] >> 3) - 1024 + 2) >> 2)); 01074 put_bits(&pbs[j], 1, enc_blks[j].dct_mode); 01075 put_bits(&pbs[j], 2, enc_blks[j].cno); 01076 01077 dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j+1]); 01078 dif += sz; 01079 } 01080 01081 /* Second pass over each MB space */ 01082 pb = &pbs[start_mb]; 01083 for (i=0; i<s->sys->bpm; i++) { 01084 if (enc_blks[start_mb+i].partial_bit_count) 01085 pb = dv_encode_ac(&enc_blks[start_mb+i], pb, &pbs[start_mb+s->sys->bpm]); 01086 } 01087 } 01088 01089 /* Third and final pass over the whole video segment space */ 01090 pb = &pbs[0]; 01091 for (j=0; j<5*s->sys->bpm; j++) { 01092 if (enc_blks[j].partial_bit_count) 01093 pb = dv_encode_ac(&enc_blks[j], pb, &pbs[s->sys->bpm*5]); 01094 if (enc_blks[j].partial_bit_count) 01095 av_log(NULL, AV_LOG_ERROR, "ac bitstream overflow\n"); 01096 } 01097 01098 for (j=0; j<5*s->sys->bpm; j++) 01099 flush_put_bits(&pbs[j]); 01100 01101 return 0; 01102 } 01103 01104 #if CONFIG_DVVIDEO_DECODER 01105 /* NOTE: exactly one frame must be given (120000 bytes for NTSC, 01106 144000 bytes for PAL - or twice those for 50Mbps) */ 01107 static int dvvideo_decode_frame(AVCodecContext *avctx, 01108 void *data, int *data_size, 01109 const uint8_t *buf, int buf_size) 01110 { 01111 DVVideoContext *s = avctx->priv_data; 01112 01113 s->sys = dv_frame_profile(buf); 01114 if (!s->sys || buf_size < s->sys->frame_size || dv_init_dynamic_tables(s->sys)) 01115 return -1; /* NOTE: we only accept several full frames */ 01116 01117 if (s->picture.data[0]) 01118 avctx->release_buffer(avctx, &s->picture); 01119 01120 s->picture.reference = 0; 01121 s->picture.key_frame = 1; 01122 s->picture.pict_type = FF_I_TYPE; 01123 avctx->pix_fmt = s->sys->pix_fmt; 01124 avctx->time_base = s->sys->time_base; 01125 avcodec_set_dimensions(avctx, s->sys->width, s->sys->height); 01126 if (avctx->get_buffer(avctx, &s->picture) < 0) { 01127 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 01128 return -1; 01129 } 01130 s->picture.interlaced_frame = 1; 01131 s->picture.top_field_first = 0; 01132 01133 s->buf = buf; 01134 avctx->execute(avctx, dv_decode_video_segment, s->sys->work_chunks, NULL, 01135 dv_work_pool_size(s->sys), sizeof(DVwork_chunk)); 01136 01137 emms_c(); 01138 01139 /* return image */ 01140 *data_size = sizeof(AVFrame); 01141 *(AVFrame*)data = s->picture; 01142 01143 return s->sys->frame_size; 01144 } 01145 #endif /* CONFIG_DVVIDEO_DECODER */ 01146 01147 01148 static inline int dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c, 01149 uint8_t* buf) 01150 { 01151 /* 01152 * Here's what SMPTE314M says about these two: 01153 * (page 6) APTn, AP1n, AP2n, AP3n: These data shall be identical 01154 * as track application IDs (APTn = 001, AP1n = 01155 * 001, AP2n = 001, AP3n = 001), if the source signal 01156 * comes from a digital VCR. If the signal source is 01157 * unknown, all bits for these data shall be set to 1. 01158 * (page 12) STYPE: STYPE defines a signal type of video signal 01159 * 00000b = 4:1:1 compression 01160 * 00100b = 4:2:2 compression 01161 * XXXXXX = Reserved 01162 * Now, I've got two problems with these statements: 01163 * 1. it looks like APT == 111b should be a safe bet, but it isn't. 01164 * It seems that for PAL as defined in IEC 61834 we have to set 01165 * APT to 000 and for SMPTE314M to 001. 01166 * 2. It is not at all clear what STYPE is used for 4:2:0 PAL 01167 * compression scheme (if any). 01168 */ 01169 int apt = (c->sys->pix_fmt == PIX_FMT_YUV420P ? 0 : 1); 01170 01171 uint8_t aspect = 0; 01172 if ((int)(av_q2d(c->avctx->sample_aspect_ratio) * c->avctx->width / c->avctx->height * 10) >= 17) /* 16:9 */ 01173 aspect = 0x02; 01174 01175 buf[0] = (uint8_t)pack_id; 01176 switch (pack_id) { 01177 case dv_header525: /* I can't imagine why these two weren't defined as real */ 01178 case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */ 01179 buf[1] = 0xf8 | /* reserved -- always 1 */ 01180 (apt & 0x07); /* APT: Track application ID */ 01181 buf[2] = (0 << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */ 01182 (0x0f << 3) | /* reserved -- always 1 */ 01183 (apt & 0x07); /* AP1: Audio application ID */ 01184 buf[3] = (0 << 7) | /* TF2: video data is 0 - valid; 1 - invalid */ 01185 (0x0f << 3) | /* reserved -- always 1 */ 01186 (apt & 0x07); /* AP2: Video application ID */ 01187 buf[4] = (0 << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */ 01188 (0x0f << 3) | /* reserved -- always 1 */ 01189 (apt & 0x07); /* AP3: Subcode application ID */ 01190 break; 01191 case dv_video_source: 01192 buf[1] = 0xff; /* reserved -- always 1 */ 01193 buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */ 01194 (1 << 6) | /* following CLF is valid - 0, invalid - 1 */ 01195 (3 << 4) | /* CLF: color frames ID (see ITU-R BT.470-4) */ 01196 0xf; /* reserved -- always 1 */ 01197 buf[3] = (3 << 6) | /* reserved -- always 1 */ 01198 (c->sys->dsf << 5) | /* system: 60fields/50fields */ 01199 c->sys->video_stype; /* signal type video compression */ 01200 buf[4] = 0xff; /* VISC: 0xff -- no information */ 01201 break; 01202 case dv_video_control: 01203 buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */ 01204 0x3f; /* reserved -- always 1 */ 01205 buf[2] = 0xc8 | /* reserved -- always b11001xxx */ 01206 aspect; 01207 buf[3] = (1 << 7) | /* frame/field flag 1 -- frame, 0 -- field */ 01208 (1 << 6) | /* first/second field flag 0 -- field 2, 1 -- field 1 */ 01209 (1 << 5) | /* frame change flag 0 -- same picture as before, 1 -- different */ 01210 (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */ 01211 0xc; /* reserved -- always b1100 */ 01212 buf[4] = 0xff; /* reserved -- always 1 */ 01213 break; 01214 default: 01215 buf[1] = buf[2] = buf[3] = buf[4] = 0xff; 01216 } 01217 return 5; 01218 } 01219 01220 #if CONFIG_DVVIDEO_ENCODER 01221 static void dv_format_frame(DVVideoContext* c, uint8_t* buf) 01222 { 01223 int chan, i, j, k; 01224 01225 for (chan = 0; chan < c->sys->n_difchan; chan++) { 01226 for (i = 0; i < c->sys->difseg_size; i++) { 01227 memset(buf, 0xff, 80 * 6); /* first 6 DIF blocks are for control data */ 01228 01229 /* DV header: 1DIF */ 01230 buf += dv_write_dif_id(dv_sect_header, chan, i, 0, buf); 01231 buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf); 01232 buf += 72; /* unused bytes */ 01233 01234 /* DV subcode: 2DIFs */ 01235 for (j = 0; j < 2; j++) { 01236 buf += dv_write_dif_id(dv_sect_subcode, chan, i, j, buf); 01237 for (k = 0; k < 6; k++) 01238 buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size/2), buf) + 5; 01239 buf += 29; /* unused bytes */ 01240 } 01241 01242 /* DV VAUX: 3DIFS */ 01243 for (j = 0; j < 3; j++) { 01244 buf += dv_write_dif_id(dv_sect_vaux, chan, i, j, buf); 01245 buf += dv_write_pack(dv_video_source, c, buf); 01246 buf += dv_write_pack(dv_video_control, c, buf); 01247 buf += 7*5; 01248 buf += dv_write_pack(dv_video_source, c, buf); 01249 buf += dv_write_pack(dv_video_control, c, buf); 01250 buf += 4*5 + 2; /* unused bytes */ 01251 } 01252 01253 /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */ 01254 for (j = 0; j < 135; j++) { 01255 if (j%15 == 0) { 01256 memset(buf, 0xff, 80); 01257 buf += dv_write_dif_id(dv_sect_audio, chan, i, j/15, buf); 01258 buf += 77; /* audio control & shuffled PCM audio */ 01259 } 01260 buf += dv_write_dif_id(dv_sect_video, chan, i, j, buf); 01261 buf += 77; /* 1 video macroblock: 1 bytes control 01262 4 * 14 bytes Y 8x8 data 01263 10 bytes Cr 8x8 data 01264 10 bytes Cb 8x8 data */ 01265 } 01266 } 01267 } 01268 } 01269 01270 01271 static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size, 01272 void *data) 01273 { 01274 DVVideoContext *s = c->priv_data; 01275 01276 s->sys = dv_codec_profile(c); 01277 if (!s->sys || buf_size < s->sys->frame_size || dv_init_dynamic_tables(s->sys)) 01278 return -1; 01279 01280 c->pix_fmt = s->sys->pix_fmt; 01281 s->picture = *((AVFrame *)data); 01282 s->picture.key_frame = 1; 01283 s->picture.pict_type = FF_I_TYPE; 01284 01285 s->buf = buf; 01286 c->execute(c, dv_encode_video_segment, s->sys->work_chunks, NULL, 01287 dv_work_pool_size(s->sys), sizeof(DVwork_chunk)); 01288 01289 emms_c(); 01290 01291 dv_format_frame(s, buf); 01292 01293 return s->sys->frame_size; 01294 } 01295 #endif 01296 01297 static int dvvideo_close(AVCodecContext *c) 01298 { 01299 DVVideoContext *s = c->priv_data; 01300 01301 if (s->picture.data[0]) 01302 c->release_buffer(c, &s->picture); 01303 01304 return 0; 01305 } 01306 01307 01308 #if CONFIG_DVVIDEO_ENCODER 01309 AVCodec dvvideo_encoder = { 01310 "dvvideo", 01311 CODEC_TYPE_VIDEO, 01312 CODEC_ID_DVVIDEO, 01313 sizeof(DVVideoContext), 01314 dvvideo_init, 01315 dvvideo_encode_frame, 01316 .pix_fmts = (enum PixelFormat[]) {PIX_FMT_YUV411P, PIX_FMT_YUV422P, PIX_FMT_YUV420P, PIX_FMT_NONE}, 01317 .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"), 01318 }; 01319 #endif // CONFIG_DVVIDEO_ENCODER 01320 01321 #if CONFIG_DVVIDEO_DECODER 01322 AVCodec dvvideo_decoder = { 01323 "dvvideo", 01324 CODEC_TYPE_VIDEO, 01325 CODEC_ID_DVVIDEO, 01326 sizeof(DVVideoContext), 01327 dvvideo_init, 01328 NULL, 01329 dvvideo_close, 01330 dvvideo_decode_frame, 01331 CODEC_CAP_DR1, 01332 NULL, 01333 .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"), 01334 }; 01335 #endif