You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2640 lines
100KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * The simplest mpeg encoder (well, it was the simplest!).
  27. */
  28. #include "libavutil/intmath.h"
  29. #include "libavutil/imgutils.h"
  30. #include "avcodec.h"
  31. #include "dsputil.h"
  32. #include "internal.h"
  33. #include "mpegvideo.h"
  34. #include "mpegvideo_common.h"
  35. #include "mjpegenc.h"
  36. #include "msmpeg4.h"
  37. #include "faandct.h"
  38. #include "xvmc_internal.h"
  39. #include "thread.h"
  40. #include <limits.h>
  41. //#undef NDEBUG
  42. //#include <assert.h>
  43. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  44. DCTELEM *block, int n, int qscale);
  45. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  46. DCTELEM *block, int n, int qscale);
  47. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  48. DCTELEM *block, int n, int qscale);
  49. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  50. DCTELEM *block, int n, int qscale);
  51. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  52. DCTELEM *block, int n, int qscale);
  53. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  54. DCTELEM *block, int n, int qscale);
  55. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  56. DCTELEM *block, int n, int qscale);
  57. /* enable all paranoid tests for rounding, overflows, etc... */
  58. //#define PARANOID
  59. //#define DEBUG
  60. static const uint8_t ff_default_chroma_qscale_table[32]={
  61. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  62. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
  63. };
  64. const uint8_t ff_mpeg1_dc_scale_table[128]={
  65. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  66. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  67. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  68. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  69. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  70. };
  71. static const uint8_t mpeg2_dc_scale_table1[128]={
  72. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  73. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  74. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  75. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  76. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  77. };
  78. static const uint8_t mpeg2_dc_scale_table2[128]={
  79. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  80. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  81. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  82. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  83. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  84. };
  85. static const uint8_t mpeg2_dc_scale_table3[128]={
  86. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  87. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  88. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  89. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  90. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  91. };
  92. const uint8_t * const ff_mpeg2_dc_scale_table[4]={
  93. ff_mpeg1_dc_scale_table,
  94. mpeg2_dc_scale_table1,
  95. mpeg2_dc_scale_table2,
  96. mpeg2_dc_scale_table3,
  97. };
  98. const enum PixelFormat ff_pixfmt_list_420[] = {
  99. PIX_FMT_YUV420P,
  100. PIX_FMT_NONE
  101. };
  102. const enum PixelFormat ff_hwaccel_pixfmt_list_420[] = {
  103. PIX_FMT_DXVA2_VLD,
  104. PIX_FMT_VAAPI_VLD,
  105. PIX_FMT_YUV420P,
  106. PIX_FMT_NONE
  107. };
  108. const uint8_t *ff_find_start_code(const uint8_t * restrict p, const uint8_t *end, uint32_t * restrict state){
  109. int i;
  110. assert(p<=end);
  111. if(p>=end)
  112. return end;
  113. for(i=0; i<3; i++){
  114. uint32_t tmp= *state << 8;
  115. *state= tmp + *(p++);
  116. if(tmp == 0x100 || p==end)
  117. return p;
  118. }
  119. while(p<end){
  120. if (p[-1] > 1 ) p+= 3;
  121. else if(p[-2] ) p+= 2;
  122. else if(p[-3]|(p[-1]-1)) p++;
  123. else{
  124. p++;
  125. break;
  126. }
  127. }
  128. p= FFMIN(p, end)-4;
  129. *state= AV_RB32(p);
  130. return p+4;
  131. }
  132. /* init common dct for both encoder and decoder */
  133. av_cold int ff_dct_common_init(MpegEncContext *s)
  134. {
  135. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  136. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  137. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  138. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  139. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  140. if(s->flags & CODEC_FLAG_BITEXACT)
  141. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  142. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  143. #if HAVE_MMX
  144. MPV_common_init_mmx(s);
  145. #elif ARCH_ALPHA
  146. MPV_common_init_axp(s);
  147. #elif CONFIG_MLIB
  148. MPV_common_init_mlib(s);
  149. #elif HAVE_MMI
  150. MPV_common_init_mmi(s);
  151. #elif ARCH_ARM
  152. MPV_common_init_arm(s);
  153. #elif HAVE_ALTIVEC
  154. MPV_common_init_altivec(s);
  155. #elif ARCH_BFIN
  156. MPV_common_init_bfin(s);
  157. #endif
  158. /* load & permutate scantables
  159. note: only wmv uses different ones
  160. */
  161. if(s->alternate_scan){
  162. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  163. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  164. }else{
  165. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  166. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  167. }
  168. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  169. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  170. return 0;
  171. }
  172. void ff_copy_picture(Picture *dst, Picture *src){
  173. *dst = *src;
  174. dst->type= FF_BUFFER_TYPE_COPY;
  175. }
  176. /**
  177. * Release a frame buffer
  178. */
  179. static void free_frame_buffer(MpegEncContext *s, Picture *pic)
  180. {
  181. ff_thread_release_buffer(s->avctx, (AVFrame*)pic);
  182. av_freep(&pic->hwaccel_picture_private);
  183. }
  184. /**
  185. * Allocate a frame buffer
  186. */
  187. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  188. {
  189. int r;
  190. if (s->avctx->hwaccel) {
  191. assert(!pic->hwaccel_picture_private);
  192. if (s->avctx->hwaccel->priv_data_size) {
  193. pic->hwaccel_picture_private = av_mallocz(s->avctx->hwaccel->priv_data_size);
  194. if (!pic->hwaccel_picture_private) {
  195. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  196. return -1;
  197. }
  198. }
  199. }
  200. r = ff_thread_get_buffer(s->avctx, (AVFrame*)pic);
  201. if (r<0 || !pic->age || !pic->type || !pic->data[0]) {
  202. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
  203. av_freep(&pic->hwaccel_picture_private);
  204. return -1;
  205. }
  206. if (s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])) {
  207. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
  208. free_frame_buffer(s, pic);
  209. return -1;
  210. }
  211. if (pic->linesize[1] != pic->linesize[2]) {
  212. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride mismatch)\n");
  213. free_frame_buffer(s, pic);
  214. return -1;
  215. }
  216. return 0;
  217. }
  218. /**
  219. * allocates a Picture
  220. * The pixels are allocated/set by calling get_buffer() if shared=0
  221. */
  222. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared){
  223. const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) does not sig11
  224. const int mb_array_size= s->mb_stride*s->mb_height;
  225. const int b8_array_size= s->b8_stride*s->mb_height*2;
  226. const int b4_array_size= s->b4_stride*s->mb_height*4;
  227. int i;
  228. int r= -1;
  229. if(shared){
  230. assert(pic->data[0]);
  231. assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
  232. pic->type= FF_BUFFER_TYPE_SHARED;
  233. }else{
  234. assert(!pic->data[0]);
  235. if (alloc_frame_buffer(s, pic) < 0)
  236. return -1;
  237. s->linesize = pic->linesize[0];
  238. s->uvlinesize= pic->linesize[1];
  239. }
  240. if(pic->qscale_table==NULL){
  241. if (s->encoding) {
  242. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_var , mb_array_size * sizeof(int16_t) , fail)
  243. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mc_mb_var, mb_array_size * sizeof(int16_t) , fail)
  244. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_mean , mb_array_size * sizeof(int8_t ) , fail)
  245. }
  246. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2, fail) //the +2 is for the slice end check
  247. FF_ALLOCZ_OR_GOTO(s->avctx, pic->qscale_table , mb_array_size * sizeof(uint8_t) , fail)
  248. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_type_base , (big_mb_num + s->mb_stride) * sizeof(uint32_t), fail)
  249. pic->mb_type= pic->mb_type_base + 2*s->mb_stride+1;
  250. if(s->out_format == FMT_H264){
  251. for(i=0; i<2; i++){
  252. FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i], 2 * (b4_array_size+4) * sizeof(int16_t), fail)
  253. pic->motion_val[i]= pic->motion_val_base[i]+4;
  254. FF_ALLOCZ_OR_GOTO(s->avctx, pic->ref_index[i], 4*mb_array_size * sizeof(uint8_t), fail)
  255. }
  256. pic->motion_subsample_log2= 2;
  257. }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
  258. for(i=0; i<2; i++){
  259. FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i], 2 * (b8_array_size+4) * sizeof(int16_t), fail)
  260. pic->motion_val[i]= pic->motion_val_base[i]+4;
  261. FF_ALLOCZ_OR_GOTO(s->avctx, pic->ref_index[i], 4*mb_array_size * sizeof(uint8_t), fail)
  262. }
  263. pic->motion_subsample_log2= 3;
  264. }
  265. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  266. FF_ALLOCZ_OR_GOTO(s->avctx, pic->dct_coeff, 64 * mb_array_size * sizeof(DCTELEM)*6, fail)
  267. }
  268. pic->qstride= s->mb_stride;
  269. FF_ALLOCZ_OR_GOTO(s->avctx, pic->pan_scan , 1 * sizeof(AVPanScan), fail)
  270. }
  271. /* It might be nicer if the application would keep track of these
  272. * but it would require an API change. */
  273. memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
  274. s->prev_pict_types[0]= s->dropable ? AV_PICTURE_TYPE_B : s->pict_type;
  275. if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == AV_PICTURE_TYPE_B)
  276. pic->age= INT_MAX; // Skipped MBs in B-frames are quite rare in MPEG-1/2 and it is a bit tricky to skip them anyway.
  277. pic->owner2 = NULL;
  278. return 0;
  279. fail: //for the FF_ALLOCZ_OR_GOTO macro
  280. if(r>=0)
  281. free_frame_buffer(s, pic);
  282. return -1;
  283. }
  284. /**
  285. * deallocates a picture
  286. */
  287. static void free_picture(MpegEncContext *s, Picture *pic){
  288. int i;
  289. if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
  290. free_frame_buffer(s, pic);
  291. }
  292. av_freep(&pic->mb_var);
  293. av_freep(&pic->mc_mb_var);
  294. av_freep(&pic->mb_mean);
  295. av_freep(&pic->mbskip_table);
  296. av_freep(&pic->qscale_table);
  297. av_freep(&pic->mb_type_base);
  298. av_freep(&pic->dct_coeff);
  299. av_freep(&pic->pan_scan);
  300. pic->mb_type= NULL;
  301. for(i=0; i<2; i++){
  302. av_freep(&pic->motion_val_base[i]);
  303. av_freep(&pic->ref_index[i]);
  304. }
  305. if(pic->type == FF_BUFFER_TYPE_SHARED){
  306. for(i=0; i<4; i++){
  307. pic->base[i]=
  308. pic->data[i]= NULL;
  309. }
  310. pic->type= 0;
  311. }
  312. }
  313. static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
  314. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  315. int c_size = s->mb_stride * (s->mb_height + 1);
  316. int yc_size = y_size + 2 * c_size;
  317. int i;
  318. // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264)
  319. FF_ALLOCZ_OR_GOTO(s->avctx, s->allocated_edge_emu_buffer, (s->width+64)*2*21*2, fail); //(width + edge + align)*interlaced*MBsize*tolerance
  320. s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*21;
  321. //FIXME should be linesize instead of s->width*2 but that is not known before get_buffer()
  322. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t), fail)
  323. s->me.temp= s->me.scratchpad;
  324. s->rd_scratchpad= s->me.scratchpad;
  325. s->b_scratchpad= s->me.scratchpad;
  326. s->obmc_scratchpad= s->me.scratchpad + 16;
  327. if (s->encoding) {
  328. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map , ME_MAP_SIZE*sizeof(uint32_t), fail)
  329. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t), fail)
  330. if(s->avctx->noise_reduction){
  331. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum, 2 * 64 * sizeof(int), fail)
  332. }
  333. }
  334. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64*12*2 * sizeof(DCTELEM), fail)
  335. s->block= s->blocks[0];
  336. for(i=0;i<12;i++){
  337. s->pblocks[i] = &s->block[i];
  338. }
  339. if (s->out_format == FMT_H263) {
  340. /* ac values */
  341. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base, yc_size * sizeof(int16_t) * 16, fail);
  342. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  343. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  344. s->ac_val[2] = s->ac_val[1] + c_size;
  345. }
  346. return 0;
  347. fail:
  348. return -1; //free() through MPV_common_end()
  349. }
  350. static void free_duplicate_context(MpegEncContext *s){
  351. if(s==NULL) return;
  352. av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
  353. av_freep(&s->me.scratchpad);
  354. s->me.temp=
  355. s->rd_scratchpad=
  356. s->b_scratchpad=
  357. s->obmc_scratchpad= NULL;
  358. av_freep(&s->dct_error_sum);
  359. av_freep(&s->me.map);
  360. av_freep(&s->me.score_map);
  361. av_freep(&s->blocks);
  362. av_freep(&s->ac_val_base);
  363. s->block= NULL;
  364. }
  365. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src){
  366. #define COPY(a) bak->a= src->a
  367. COPY(allocated_edge_emu_buffer);
  368. COPY(edge_emu_buffer);
  369. COPY(me.scratchpad);
  370. COPY(me.temp);
  371. COPY(rd_scratchpad);
  372. COPY(b_scratchpad);
  373. COPY(obmc_scratchpad);
  374. COPY(me.map);
  375. COPY(me.score_map);
  376. COPY(blocks);
  377. COPY(block);
  378. COPY(start_mb_y);
  379. COPY(end_mb_y);
  380. COPY(me.map_generation);
  381. COPY(pb);
  382. COPY(dct_error_sum);
  383. COPY(dct_count[0]);
  384. COPY(dct_count[1]);
  385. COPY(ac_val_base);
  386. COPY(ac_val[0]);
  387. COPY(ac_val[1]);
  388. COPY(ac_val[2]);
  389. #undef COPY
  390. }
  391. void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
  392. MpegEncContext bak;
  393. int i;
  394. //FIXME copy only needed parts
  395. //START_TIMER
  396. backup_duplicate_context(&bak, dst);
  397. memcpy(dst, src, sizeof(MpegEncContext));
  398. backup_duplicate_context(dst, &bak);
  399. for(i=0;i<12;i++){
  400. dst->pblocks[i] = &dst->block[i];
  401. }
  402. //STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  403. }
  404. int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  405. {
  406. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  407. if(dst == src || !s1->context_initialized) return 0;
  408. //FIXME can parameters change on I-frames? in that case dst may need a reinit
  409. if(!s->context_initialized){
  410. memcpy(s, s1, sizeof(MpegEncContext));
  411. s->avctx = dst;
  412. s->picture_range_start += MAX_PICTURE_COUNT;
  413. s->picture_range_end += MAX_PICTURE_COUNT;
  414. s->bitstream_buffer = NULL;
  415. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  416. MPV_common_init(s);
  417. }
  418. s->avctx->coded_height = s1->avctx->coded_height;
  419. s->avctx->coded_width = s1->avctx->coded_width;
  420. s->avctx->width = s1->avctx->width;
  421. s->avctx->height = s1->avctx->height;
  422. s->coded_picture_number = s1->coded_picture_number;
  423. s->picture_number = s1->picture_number;
  424. s->input_picture_number = s1->input_picture_number;
  425. memcpy(s->picture, s1->picture, s1->picture_count * sizeof(Picture));
  426. memcpy(&s->last_picture, &s1->last_picture, (char*)&s1->last_picture_ptr - (char*)&s1->last_picture);
  427. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  428. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  429. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  430. memcpy(s->prev_pict_types, s1->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
  431. //Error/bug resilience
  432. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  433. s->workaround_bugs = s1->workaround_bugs;
  434. //MPEG4 timing info
  435. memcpy(&s->time_increment_bits, &s1->time_increment_bits, (char*)&s1->shape - (char*)&s1->time_increment_bits);
  436. //B-frame info
  437. s->max_b_frames = s1->max_b_frames;
  438. s->low_delay = s1->low_delay;
  439. s->dropable = s1->dropable;
  440. //DivX handling (doesn't work)
  441. s->divx_packed = s1->divx_packed;
  442. if(s1->bitstream_buffer){
  443. if (s1->bitstream_buffer_size + FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  444. av_fast_malloc(&s->bitstream_buffer, &s->allocated_bitstream_buffer_size, s1->allocated_bitstream_buffer_size);
  445. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  446. memcpy(s->bitstream_buffer, s1->bitstream_buffer, s1->bitstream_buffer_size);
  447. memset(s->bitstream_buffer+s->bitstream_buffer_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  448. }
  449. //MPEG2/interlacing info
  450. memcpy(&s->progressive_sequence, &s1->progressive_sequence, (char*)&s1->rtp_mode - (char*)&s1->progressive_sequence);
  451. if(!s1->first_field){
  452. s->last_pict_type= s1->pict_type;
  453. if (s1->current_picture_ptr) s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->quality;
  454. if(s1->pict_type!=AV_PICTURE_TYPE_B){
  455. s->last_non_b_pict_type= s1->pict_type;
  456. }
  457. }
  458. return 0;
  459. }
  460. /**
  461. * sets the given MpegEncContext to common defaults (same for encoding and decoding).
  462. * the changed fields will not depend upon the prior state of the MpegEncContext.
  463. */
  464. void MPV_common_defaults(MpegEncContext *s){
  465. s->y_dc_scale_table=
  466. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  467. s->chroma_qscale_table= ff_default_chroma_qscale_table;
  468. s->progressive_frame= 1;
  469. s->progressive_sequence= 1;
  470. s->picture_structure= PICT_FRAME;
  471. s->coded_picture_number = 0;
  472. s->picture_number = 0;
  473. s->input_picture_number = 0;
  474. s->picture_in_gop_number = 0;
  475. s->f_code = 1;
  476. s->b_code = 1;
  477. s->picture_range_start = 0;
  478. s->picture_range_end = MAX_PICTURE_COUNT;
  479. }
  480. /**
  481. * sets the given MpegEncContext to defaults for decoding.
  482. * the changed fields will not depend upon the prior state of the MpegEncContext.
  483. */
  484. void MPV_decode_defaults(MpegEncContext *s){
  485. MPV_common_defaults(s);
  486. }
  487. /**
  488. * init common structure for both encoder and decoder.
  489. * this assumes that some variables like width/height are already set
  490. */
  491. av_cold int MPV_common_init(MpegEncContext *s)
  492. {
  493. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y, threads;
  494. if(s->codec_id == CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  495. s->mb_height = (s->height + 31) / 32 * 2;
  496. else if (s->codec_id != CODEC_ID_H264)
  497. s->mb_height = (s->height + 15) / 16;
  498. if(s->avctx->pix_fmt == PIX_FMT_NONE){
  499. av_log(s->avctx, AV_LOG_ERROR, "decoding to PIX_FMT_NONE is not supported.\n");
  500. return -1;
  501. }
  502. if(s->avctx->active_thread_type&FF_THREAD_SLICE &&
  503. (s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height))){
  504. av_log(s->avctx, AV_LOG_ERROR, "too many threads\n");
  505. return -1;
  506. }
  507. if((s->width || s->height) && av_image_check_size(s->width, s->height, 0, s->avctx))
  508. return -1;
  509. dsputil_init(&s->dsp, s->avctx);
  510. ff_dct_common_init(s);
  511. s->flags= s->avctx->flags;
  512. s->flags2= s->avctx->flags2;
  513. s->mb_width = (s->width + 15) / 16;
  514. s->mb_stride = s->mb_width + 1;
  515. s->b8_stride = s->mb_width*2 + 1;
  516. s->b4_stride = s->mb_width*4 + 1;
  517. mb_array_size= s->mb_height * s->mb_stride;
  518. mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
  519. /* set chroma shifts */
  520. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),
  521. &(s->chroma_y_shift) );
  522. /* set default edge pos, will be overriden in decode_header if needed */
  523. s->h_edge_pos= s->mb_width*16;
  524. s->v_edge_pos= s->mb_height*16;
  525. s->mb_num = s->mb_width * s->mb_height;
  526. s->block_wrap[0]=
  527. s->block_wrap[1]=
  528. s->block_wrap[2]=
  529. s->block_wrap[3]= s->b8_stride;
  530. s->block_wrap[4]=
  531. s->block_wrap[5]= s->mb_stride;
  532. y_size = s->b8_stride * (2 * s->mb_height + 1);
  533. c_size = s->mb_stride * (s->mb_height + 1);
  534. yc_size = y_size + 2 * c_size;
  535. /* convert fourcc to upper case */
  536. s->codec_tag = ff_toupper4(s->avctx->codec_tag);
  537. s->stream_codec_tag = ff_toupper4(s->avctx->stream_codec_tag);
  538. s->avctx->coded_frame= (AVFrame*)&s->current_picture;
  539. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num+1)*sizeof(int), fail) //error ressilience code looks cleaner with this
  540. for(y=0; y<s->mb_height; y++){
  541. for(x=0; x<s->mb_width; x++){
  542. s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
  543. }
  544. }
  545. s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
  546. if (s->encoding) {
  547. /* Allocate MV tables */
  548. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  549. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  550. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  551. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  552. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  553. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  554. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  555. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  556. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  557. s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  558. s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  559. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  560. if(s->msmpeg4_version){
  561. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int), fail);
  562. }
  563. FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  564. /* Allocate MB type table */
  565. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type , mb_array_size * sizeof(uint16_t), fail) //needed for encoding
  566. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
  567. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix , 64*32 * sizeof(int), fail)
  568. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix , 64*32 * sizeof(int), fail)
  569. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t), fail)
  570. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t), fail)
  571. FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*), fail)
  572. FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*), fail)
  573. if(s->avctx->noise_reduction){
  574. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail)
  575. }
  576. }
  577. s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count);
  578. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture, s->picture_count * sizeof(Picture), fail)
  579. for(i = 0; i < s->picture_count; i++) {
  580. avcodec_get_frame_defaults((AVFrame *)&s->picture[i]);
  581. }
  582. FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table, mb_array_size*sizeof(uint8_t), fail)
  583. if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
  584. /* interlaced direct mode decoding tables */
  585. for(i=0; i<2; i++){
  586. int j, k;
  587. for(j=0; j<2; j++){
  588. for(k=0; k<2; k++){
  589. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_mv_table_base[i][j][k], mv_table_size * 2 * sizeof(int16_t), fail)
  590. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
  591. }
  592. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
  593. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
  594. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]+ s->mb_stride + 1;
  595. }
  596. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
  597. }
  598. }
  599. if (s->out_format == FMT_H263) {
  600. /* cbp values */
  601. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  602. s->coded_block= s->coded_block_base + s->b8_stride + 1;
  603. /* cbp, ac_pred, pred_dir */
  604. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail)
  605. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail)
  606. }
  607. if (s->h263_pred || s->h263_plus || !s->encoding) {
  608. /* dc values */
  609. //MN: we need these for error resilience of intra-frames
  610. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
  611. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  612. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  613. s->dc_val[2] = s->dc_val[1] + c_size;
  614. for(i=0;i<yc_size;i++)
  615. s->dc_val_base[i] = 1024;
  616. }
  617. /* which mb is a intra block */
  618. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  619. memset(s->mbintra_table, 1, mb_array_size);
  620. /* init macroblock skip table */
  621. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size+2, fail);
  622. //Note the +1 is for a quicker mpeg4 slice_end detection
  623. FF_ALLOCZ_OR_GOTO(s->avctx, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE, fail);
  624. s->parse_context.state= -1;
  625. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  626. s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  627. s->visualization_buffer[1] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  628. s->visualization_buffer[2] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  629. }
  630. s->context_initialized = 1;
  631. s->thread_context[0]= s;
  632. if (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE) {
  633. threads = s->avctx->thread_count;
  634. for(i=1; i<threads; i++){
  635. s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
  636. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  637. }
  638. for(i=0; i<threads; i++){
  639. if(init_duplicate_context(s->thread_context[i], s) < 0)
  640. goto fail;
  641. s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count;
  642. s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
  643. }
  644. } else {
  645. if(init_duplicate_context(s, s) < 0) goto fail;
  646. s->start_mb_y = 0;
  647. s->end_mb_y = s->mb_height;
  648. }
  649. return 0;
  650. fail:
  651. MPV_common_end(s);
  652. return -1;
  653. }
  654. /* init common structure for both encoder and decoder */
  655. void MPV_common_end(MpegEncContext *s)
  656. {
  657. int i, j, k;
  658. if (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE) {
  659. for(i=0; i<s->avctx->thread_count; i++){
  660. free_duplicate_context(s->thread_context[i]);
  661. }
  662. for(i=1; i<s->avctx->thread_count; i++){
  663. av_freep(&s->thread_context[i]);
  664. }
  665. } else free_duplicate_context(s);
  666. av_freep(&s->parse_context.buffer);
  667. s->parse_context.buffer_size=0;
  668. av_freep(&s->mb_type);
  669. av_freep(&s->p_mv_table_base);
  670. av_freep(&s->b_forw_mv_table_base);
  671. av_freep(&s->b_back_mv_table_base);
  672. av_freep(&s->b_bidir_forw_mv_table_base);
  673. av_freep(&s->b_bidir_back_mv_table_base);
  674. av_freep(&s->b_direct_mv_table_base);
  675. s->p_mv_table= NULL;
  676. s->b_forw_mv_table= NULL;
  677. s->b_back_mv_table= NULL;
  678. s->b_bidir_forw_mv_table= NULL;
  679. s->b_bidir_back_mv_table= NULL;
  680. s->b_direct_mv_table= NULL;
  681. for(i=0; i<2; i++){
  682. for(j=0; j<2; j++){
  683. for(k=0; k<2; k++){
  684. av_freep(&s->b_field_mv_table_base[i][j][k]);
  685. s->b_field_mv_table[i][j][k]=NULL;
  686. }
  687. av_freep(&s->b_field_select_table[i][j]);
  688. av_freep(&s->p_field_mv_table_base[i][j]);
  689. s->p_field_mv_table[i][j]=NULL;
  690. }
  691. av_freep(&s->p_field_select_table[i]);
  692. }
  693. av_freep(&s->dc_val_base);
  694. av_freep(&s->coded_block_base);
  695. av_freep(&s->mbintra_table);
  696. av_freep(&s->cbp_table);
  697. av_freep(&s->pred_dir_table);
  698. av_freep(&s->mbskip_table);
  699. av_freep(&s->prev_pict_types);
  700. av_freep(&s->bitstream_buffer);
  701. s->allocated_bitstream_buffer_size=0;
  702. av_freep(&s->avctx->stats_out);
  703. av_freep(&s->ac_stats);
  704. av_freep(&s->error_status_table);
  705. av_freep(&s->mb_index2xy);
  706. av_freep(&s->lambda_table);
  707. av_freep(&s->q_intra_matrix);
  708. av_freep(&s->q_inter_matrix);
  709. av_freep(&s->q_intra_matrix16);
  710. av_freep(&s->q_inter_matrix16);
  711. av_freep(&s->input_picture);
  712. av_freep(&s->reordered_input_picture);
  713. av_freep(&s->dct_offset);
  714. if(s->picture && !s->avctx->is_copy){
  715. for(i=0; i<s->picture_count; i++){
  716. free_picture(s, &s->picture[i]);
  717. }
  718. }
  719. av_freep(&s->picture);
  720. s->context_initialized = 0;
  721. s->last_picture_ptr=
  722. s->next_picture_ptr=
  723. s->current_picture_ptr= NULL;
  724. s->linesize= s->uvlinesize= 0;
  725. for(i=0; i<3; i++)
  726. av_freep(&s->visualization_buffer[i]);
  727. if(!(s->avctx->active_thread_type&FF_THREAD_FRAME))
  728. avcodec_default_free_buffers(s->avctx);
  729. }
  730. void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3])
  731. {
  732. int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
  733. uint8_t index_run[MAX_RUN+1];
  734. int last, run, level, start, end, i;
  735. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  736. if(static_store && rl->max_level[0])
  737. return;
  738. /* compute max_level[], max_run[] and index_run[] */
  739. for(last=0;last<2;last++) {
  740. if (last == 0) {
  741. start = 0;
  742. end = rl->last;
  743. } else {
  744. start = rl->last;
  745. end = rl->n;
  746. }
  747. memset(max_level, 0, MAX_RUN + 1);
  748. memset(max_run, 0, MAX_LEVEL + 1);
  749. memset(index_run, rl->n, MAX_RUN + 1);
  750. for(i=start;i<end;i++) {
  751. run = rl->table_run[i];
  752. level = rl->table_level[i];
  753. if (index_run[run] == rl->n)
  754. index_run[run] = i;
  755. if (level > max_level[run])
  756. max_level[run] = level;
  757. if (run > max_run[level])
  758. max_run[level] = run;
  759. }
  760. if(static_store)
  761. rl->max_level[last] = static_store[last];
  762. else
  763. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  764. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  765. if(static_store)
  766. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  767. else
  768. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  769. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  770. if(static_store)
  771. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  772. else
  773. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  774. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  775. }
  776. }
  777. void init_vlc_rl(RLTable *rl)
  778. {
  779. int i, q;
  780. for(q=0; q<32; q++){
  781. int qmul= q*2;
  782. int qadd= (q-1)|1;
  783. if(q==0){
  784. qmul=1;
  785. qadd=0;
  786. }
  787. for(i=0; i<rl->vlc.table_size; i++){
  788. int code= rl->vlc.table[i][0];
  789. int len = rl->vlc.table[i][1];
  790. int level, run;
  791. if(len==0){ // illegal code
  792. run= 66;
  793. level= MAX_LEVEL;
  794. }else if(len<0){ //more bits needed
  795. run= 0;
  796. level= code;
  797. }else{
  798. if(code==rl->n){ //esc
  799. run= 66;
  800. level= 0;
  801. }else{
  802. run= rl->table_run [code] + 1;
  803. level= rl->table_level[code] * qmul + qadd;
  804. if(code >= rl->last) run+=192;
  805. }
  806. }
  807. rl->rl_vlc[q][i].len= len;
  808. rl->rl_vlc[q][i].level= level;
  809. rl->rl_vlc[q][i].run= run;
  810. }
  811. }
  812. }
  813. void ff_release_unused_pictures(MpegEncContext *s, int remove_current)
  814. {
  815. int i;
  816. /* release non reference frames */
  817. for(i=0; i<s->picture_count; i++){
  818. if(s->picture[i].data[0] && !s->picture[i].reference
  819. && (!s->picture[i].owner2 || s->picture[i].owner2 == s)
  820. && (remove_current || &s->picture[i] != s->current_picture_ptr)
  821. /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  822. free_frame_buffer(s, &s->picture[i]);
  823. }
  824. }
  825. }
  826. int ff_find_unused_picture(MpegEncContext *s, int shared){
  827. int i;
  828. if(shared){
  829. for(i=s->picture_range_start; i<s->picture_range_end; i++){
  830. if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
  831. }
  832. }else{
  833. for(i=s->picture_range_start; i<s->picture_range_end; i++){
  834. if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
  835. }
  836. for(i=s->picture_range_start; i<s->picture_range_end; i++){
  837. if(s->picture[i].data[0]==NULL) return i;
  838. }
  839. }
  840. av_log(s->avctx, AV_LOG_FATAL, "Internal error, picture buffer overflow\n");
  841. /* We could return -1, but the codec would crash trying to draw into a
  842. * non-existing frame anyway. This is safer than waiting for a random crash.
  843. * Also the return of this is never useful, an encoder must only allocate
  844. * as much as allowed in the specification. This has no relationship to how
  845. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  846. * enough for such valid streams).
  847. * Plus, a decoder has to check stream validity and remove frames if too
  848. * many reference frames are around. Waiting for "OOM" is not correct at
  849. * all. Similarly, missing reference frames have to be replaced by
  850. * interpolated/MC frames, anything else is a bug in the codec ...
  851. */
  852. abort();
  853. return -1;
  854. }
  855. static void update_noise_reduction(MpegEncContext *s){
  856. int intra, i;
  857. for(intra=0; intra<2; intra++){
  858. if(s->dct_count[intra] > (1<<16)){
  859. for(i=0; i<64; i++){
  860. s->dct_error_sum[intra][i] >>=1;
  861. }
  862. s->dct_count[intra] >>= 1;
  863. }
  864. for(i=0; i<64; i++){
  865. s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
  866. }
  867. }
  868. }
  869. /**
  870. * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
  871. */
  872. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  873. {
  874. int i;
  875. Picture *pic;
  876. s->mb_skipped = 0;
  877. assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
  878. /* mark&release old frames */
  879. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr && s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr->data[0]) {
  880. if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
  881. free_frame_buffer(s, s->last_picture_ptr);
  882. /* release forgotten pictures */
  883. /* if(mpeg124/h263) */
  884. if(!s->encoding){
  885. for(i=0; i<s->picture_count; i++){
  886. if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
  887. av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
  888. free_frame_buffer(s, &s->picture[i]);
  889. }
  890. }
  891. }
  892. }
  893. }
  894. if(!s->encoding){
  895. ff_release_unused_pictures(s, 1);
  896. if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
  897. pic= s->current_picture_ptr; //we already have a unused image (maybe it was set before reading the header)
  898. else{
  899. i= ff_find_unused_picture(s, 0);
  900. pic= &s->picture[i];
  901. }
  902. pic->reference= 0;
  903. if (!s->dropable){
  904. if (s->codec_id == CODEC_ID_H264)
  905. pic->reference = s->picture_structure;
  906. else if (s->pict_type != AV_PICTURE_TYPE_B)
  907. pic->reference = 3;
  908. }
  909. pic->coded_picture_number= s->coded_picture_number++;
  910. if(ff_alloc_picture(s, pic, 0) < 0)
  911. return -1;
  912. s->current_picture_ptr= pic;
  913. //FIXME use only the vars from current_pic
  914. s->current_picture_ptr->top_field_first= s->top_field_first;
  915. if(s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO) {
  916. if(s->picture_structure != PICT_FRAME)
  917. s->current_picture_ptr->top_field_first= (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  918. }
  919. s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
  920. s->current_picture_ptr->field_picture= s->picture_structure != PICT_FRAME;
  921. }
  922. s->current_picture_ptr->pict_type= s->pict_type;
  923. // if(s->flags && CODEC_FLAG_QSCALE)
  924. // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
  925. s->current_picture_ptr->key_frame= s->pict_type == AV_PICTURE_TYPE_I;
  926. ff_copy_picture(&s->current_picture, s->current_picture_ptr);
  927. if (s->pict_type != AV_PICTURE_TYPE_B) {
  928. s->last_picture_ptr= s->next_picture_ptr;
  929. if(!s->dropable)
  930. s->next_picture_ptr= s->current_picture_ptr;
  931. }
  932. /* av_log(s->avctx, AV_LOG_DEBUG, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n", s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  933. s->last_picture_ptr ? s->last_picture_ptr->data[0] : NULL,
  934. s->next_picture_ptr ? s->next_picture_ptr->data[0] : NULL,
  935. s->current_picture_ptr ? s->current_picture_ptr->data[0] : NULL,
  936. s->pict_type, s->dropable);*/
  937. if(s->codec_id != CODEC_ID_H264){
  938. if((s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL) &&
  939. (s->pict_type!=AV_PICTURE_TYPE_I || s->picture_structure != PICT_FRAME)){
  940. if (s->pict_type != AV_PICTURE_TYPE_I)
  941. av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
  942. else if (s->picture_structure != PICT_FRAME)
  943. av_log(avctx, AV_LOG_INFO, "allocate dummy last picture for field based first keyframe\n");
  944. /* Allocate a dummy frame */
  945. i= ff_find_unused_picture(s, 0);
  946. s->last_picture_ptr= &s->picture[i];
  947. if(ff_alloc_picture(s, s->last_picture_ptr, 0) < 0)
  948. return -1;
  949. ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 0);
  950. ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 1);
  951. }
  952. if((s->next_picture_ptr==NULL || s->next_picture_ptr->data[0]==NULL) && s->pict_type==AV_PICTURE_TYPE_B){
  953. /* Allocate a dummy frame */
  954. i= ff_find_unused_picture(s, 0);
  955. s->next_picture_ptr= &s->picture[i];
  956. if(ff_alloc_picture(s, s->next_picture_ptr, 0) < 0)
  957. return -1;
  958. ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 0);
  959. ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 1);
  960. }
  961. }
  962. if(s->last_picture_ptr) ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  963. if(s->next_picture_ptr) ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  964. assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
  965. if(s->picture_structure!=PICT_FRAME && s->out_format != FMT_H264){
  966. int i;
  967. for(i=0; i<4; i++){
  968. if(s->picture_structure == PICT_BOTTOM_FIELD){
  969. s->current_picture.data[i] += s->current_picture.linesize[i];
  970. }
  971. s->current_picture.linesize[i] *= 2;
  972. s->last_picture.linesize[i] *=2;
  973. s->next_picture.linesize[i] *=2;
  974. }
  975. }
  976. s->error_recognition= avctx->error_recognition;
  977. /* set dequantizer, we can't do it during init as it might change for mpeg4
  978. and we can't do it in the header decode as init is not called for mpeg4 there yet */
  979. if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
  980. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  981. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  982. }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  983. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  984. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  985. }else{
  986. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  987. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  988. }
  989. if(s->dct_error_sum){
  990. assert(s->avctx->noise_reduction && s->encoding);
  991. update_noise_reduction(s);
  992. }
  993. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  994. return ff_xvmc_field_start(s, avctx);
  995. return 0;
  996. }
  997. /* generic function for encode/decode called after a frame has been coded/decoded */
  998. void MPV_frame_end(MpegEncContext *s)
  999. {
  1000. int i;
  1001. /* redraw edges for the frame if decoding didn't complete */
  1002. //just to make sure that all data is rendered.
  1003. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1004. ff_xvmc_field_end(s);
  1005. }else if((s->error_count || s->encoding || !(s->avctx->codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND))
  1006. && !s->avctx->hwaccel
  1007. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1008. && s->unrestricted_mv
  1009. && s->current_picture.reference
  1010. && !s->intra_only
  1011. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  1012. int edges = EDGE_BOTTOM | EDGE_TOP, h = s->v_edge_pos;
  1013. s->dsp.draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , h , EDGE_WIDTH , edges);
  1014. s->dsp.draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, h>>1, EDGE_WIDTH/2, edges);
  1015. s->dsp.draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, h>>1, EDGE_WIDTH/2, edges);
  1016. }
  1017. emms_c();
  1018. s->last_pict_type = s->pict_type;
  1019. s->last_lambda_for[s->pict_type]= s->current_picture_ptr->quality;
  1020. if(s->pict_type!=AV_PICTURE_TYPE_B){
  1021. s->last_non_b_pict_type= s->pict_type;
  1022. }
  1023. #if 0
  1024. /* copy back current_picture variables */
  1025. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1026. if(s->picture[i].data[0] == s->current_picture.data[0]){
  1027. s->picture[i]= s->current_picture;
  1028. break;
  1029. }
  1030. }
  1031. assert(i<MAX_PICTURE_COUNT);
  1032. #endif
  1033. if(s->encoding){
  1034. /* release non-reference frames */
  1035. for(i=0; i<s->picture_count; i++){
  1036. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  1037. free_frame_buffer(s, &s->picture[i]);
  1038. }
  1039. }
  1040. }
  1041. // clear copies, to avoid confusion
  1042. #if 0
  1043. memset(&s->last_picture, 0, sizeof(Picture));
  1044. memset(&s->next_picture, 0, sizeof(Picture));
  1045. memset(&s->current_picture, 0, sizeof(Picture));
  1046. #endif
  1047. s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
  1048. if (s->codec_id != CODEC_ID_H264 && s->current_picture.reference) {
  1049. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_height-1, 0);
  1050. }
  1051. }
  1052. /**
  1053. * draws an line from (ex, ey) -> (sx, sy).
  1054. * @param w width of the image
  1055. * @param h height of the image
  1056. * @param stride stride/linesize of the image
  1057. * @param color color of the arrow
  1058. */
  1059. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1060. int x, y, fr, f;
  1061. sx= av_clip(sx, 0, w-1);
  1062. sy= av_clip(sy, 0, h-1);
  1063. ex= av_clip(ex, 0, w-1);
  1064. ey= av_clip(ey, 0, h-1);
  1065. buf[sy*stride + sx]+= color;
  1066. if(FFABS(ex - sx) > FFABS(ey - sy)){
  1067. if(sx > ex){
  1068. FFSWAP(int, sx, ex);
  1069. FFSWAP(int, sy, ey);
  1070. }
  1071. buf+= sx + sy*stride;
  1072. ex-= sx;
  1073. f= ((ey-sy)<<16)/ex;
  1074. for(x= 0; x <= ex; x++){
  1075. y = (x*f)>>16;
  1076. fr= (x*f)&0xFFFF;
  1077. buf[ y *stride + x]+= (color*(0x10000-fr))>>16;
  1078. buf[(y+1)*stride + x]+= (color* fr )>>16;
  1079. }
  1080. }else{
  1081. if(sy > ey){
  1082. FFSWAP(int, sx, ex);
  1083. FFSWAP(int, sy, ey);
  1084. }
  1085. buf+= sx + sy*stride;
  1086. ey-= sy;
  1087. if(ey) f= ((ex-sx)<<16)/ey;
  1088. else f= 0;
  1089. for(y= 0; y <= ey; y++){
  1090. x = (y*f)>>16;
  1091. fr= (y*f)&0xFFFF;
  1092. buf[y*stride + x ]+= (color*(0x10000-fr))>>16;
  1093. buf[y*stride + x+1]+= (color* fr )>>16;
  1094. }
  1095. }
  1096. }
  1097. /**
  1098. * draws an arrow from (ex, ey) -> (sx, sy).
  1099. * @param w width of the image
  1100. * @param h height of the image
  1101. * @param stride stride/linesize of the image
  1102. * @param color color of the arrow
  1103. */
  1104. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1105. int dx,dy;
  1106. sx= av_clip(sx, -100, w+100);
  1107. sy= av_clip(sy, -100, h+100);
  1108. ex= av_clip(ex, -100, w+100);
  1109. ey= av_clip(ey, -100, h+100);
  1110. dx= ex - sx;
  1111. dy= ey - sy;
  1112. if(dx*dx + dy*dy > 3*3){
  1113. int rx= dx + dy;
  1114. int ry= -dx + dy;
  1115. int length= ff_sqrt((rx*rx + ry*ry)<<8);
  1116. //FIXME subpixel accuracy
  1117. rx= ROUNDED_DIV(rx*3<<4, length);
  1118. ry= ROUNDED_DIV(ry*3<<4, length);
  1119. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1120. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1121. }
  1122. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1123. }
  1124. /**
  1125. * prints debuging info for the given picture.
  1126. */
  1127. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
  1128. if(s->avctx->hwaccel || !pict || !pict->mb_type) return;
  1129. if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
  1130. int x,y;
  1131. av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
  1132. switch (pict->pict_type) {
  1133. case AV_PICTURE_TYPE_I: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break;
  1134. case AV_PICTURE_TYPE_P: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break;
  1135. case AV_PICTURE_TYPE_B: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break;
  1136. case AV_PICTURE_TYPE_S: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break;
  1137. case AV_PICTURE_TYPE_SI: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break;
  1138. case AV_PICTURE_TYPE_SP: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break;
  1139. }
  1140. for(y=0; y<s->mb_height; y++){
  1141. for(x=0; x<s->mb_width; x++){
  1142. if(s->avctx->debug&FF_DEBUG_SKIP){
  1143. int count= s->mbskip_table[x + y*s->mb_stride];
  1144. if(count>9) count=9;
  1145. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1146. }
  1147. if(s->avctx->debug&FF_DEBUG_QP){
  1148. av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
  1149. }
  1150. if(s->avctx->debug&FF_DEBUG_MB_TYPE){
  1151. int mb_type= pict->mb_type[x + y*s->mb_stride];
  1152. //Type & MV direction
  1153. if(IS_PCM(mb_type))
  1154. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1155. else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1156. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1157. else if(IS_INTRA4x4(mb_type))
  1158. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1159. else if(IS_INTRA16x16(mb_type))
  1160. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1161. else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1162. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1163. else if(IS_DIRECT(mb_type))
  1164. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1165. else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
  1166. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1167. else if(IS_GMC(mb_type))
  1168. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1169. else if(IS_SKIP(mb_type))
  1170. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1171. else if(!USES_LIST(mb_type, 1))
  1172. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1173. else if(!USES_LIST(mb_type, 0))
  1174. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1175. else{
  1176. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1177. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1178. }
  1179. //segmentation
  1180. if(IS_8X8(mb_type))
  1181. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1182. else if(IS_16X8(mb_type))
  1183. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1184. else if(IS_8X16(mb_type))
  1185. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1186. else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
  1187. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1188. else
  1189. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1190. if(IS_INTERLACED(mb_type))
  1191. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1192. else
  1193. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1194. }
  1195. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1196. }
  1197. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1198. }
  1199. }
  1200. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  1201. const int shift= 1 + s->quarter_sample;
  1202. int mb_y;
  1203. uint8_t *ptr;
  1204. int i;
  1205. int h_chroma_shift, v_chroma_shift, block_height;
  1206. const int width = s->avctx->width;
  1207. const int height= s->avctx->height;
  1208. const int mv_sample_log2= 4 - pict->motion_subsample_log2;
  1209. const int mv_stride= (s->mb_width << mv_sample_log2) + (s->codec_id == CODEC_ID_H264 ? 0 : 1);
  1210. s->low_delay=0; //needed to see the vectors without trashing the buffers
  1211. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1212. for(i=0; i<3; i++){
  1213. memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*height:pict->linesize[i]*height >> v_chroma_shift);
  1214. pict->data[i]= s->visualization_buffer[i];
  1215. }
  1216. pict->type= FF_BUFFER_TYPE_COPY;
  1217. ptr= pict->data[0];
  1218. block_height = 16>>v_chroma_shift;
  1219. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  1220. int mb_x;
  1221. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  1222. const int mb_index= mb_x + mb_y*s->mb_stride;
  1223. if((s->avctx->debug_mv) && pict->motion_val){
  1224. int type;
  1225. for(type=0; type<3; type++){
  1226. int direction = 0;
  1227. switch (type) {
  1228. case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=AV_PICTURE_TYPE_P))
  1229. continue;
  1230. direction = 0;
  1231. break;
  1232. case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=AV_PICTURE_TYPE_B))
  1233. continue;
  1234. direction = 0;
  1235. break;
  1236. case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=AV_PICTURE_TYPE_B))
  1237. continue;
  1238. direction = 1;
  1239. break;
  1240. }
  1241. if(!USES_LIST(pict->mb_type[mb_index], direction))
  1242. continue;
  1243. if(IS_8X8(pict->mb_type[mb_index])){
  1244. int i;
  1245. for(i=0; i<4; i++){
  1246. int sx= mb_x*16 + 4 + 8*(i&1);
  1247. int sy= mb_y*16 + 4 + 8*(i>>1);
  1248. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1249. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1250. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1251. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1252. }
  1253. }else if(IS_16X8(pict->mb_type[mb_index])){
  1254. int i;
  1255. for(i=0; i<2; i++){
  1256. int sx=mb_x*16 + 8;
  1257. int sy=mb_y*16 + 4 + 8*i;
  1258. int xy= (mb_x*2 + (mb_y*2 + i)*mv_stride) << (mv_sample_log2-1);
  1259. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1260. int my=(pict->motion_val[direction][xy][1]>>shift);
  1261. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1262. my*=2;
  1263. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1264. }
  1265. }else if(IS_8X16(pict->mb_type[mb_index])){
  1266. int i;
  1267. for(i=0; i<2; i++){
  1268. int sx=mb_x*16 + 4 + 8*i;
  1269. int sy=mb_y*16 + 8;
  1270. int xy= (mb_x*2 + i + mb_y*2*mv_stride) << (mv_sample_log2-1);
  1271. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1272. int my=(pict->motion_val[direction][xy][1]>>shift);
  1273. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1274. my*=2;
  1275. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1276. }
  1277. }else{
  1278. int sx= mb_x*16 + 8;
  1279. int sy= mb_y*16 + 8;
  1280. int xy= (mb_x + mb_y*mv_stride) << mv_sample_log2;
  1281. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1282. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1283. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1284. }
  1285. }
  1286. }
  1287. if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
  1288. uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
  1289. int y;
  1290. for(y=0; y<block_height; y++){
  1291. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= c;
  1292. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= c;
  1293. }
  1294. }
  1295. if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
  1296. int mb_type= pict->mb_type[mb_index];
  1297. uint64_t u,v;
  1298. int y;
  1299. #define COLOR(theta, r)\
  1300. u= (int)(128 + r*cos(theta*3.141592/180));\
  1301. v= (int)(128 + r*sin(theta*3.141592/180));
  1302. u=v=128;
  1303. if(IS_PCM(mb_type)){
  1304. COLOR(120,48)
  1305. }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
  1306. COLOR(30,48)
  1307. }else if(IS_INTRA4x4(mb_type)){
  1308. COLOR(90,48)
  1309. }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
  1310. // COLOR(120,48)
  1311. }else if(IS_DIRECT(mb_type)){
  1312. COLOR(150,48)
  1313. }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
  1314. COLOR(170,48)
  1315. }else if(IS_GMC(mb_type)){
  1316. COLOR(190,48)
  1317. }else if(IS_SKIP(mb_type)){
  1318. // COLOR(180,48)
  1319. }else if(!USES_LIST(mb_type, 1)){
  1320. COLOR(240,48)
  1321. }else if(!USES_LIST(mb_type, 0)){
  1322. COLOR(0,48)
  1323. }else{
  1324. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1325. COLOR(300,48)
  1326. }
  1327. u*= 0x0101010101010101ULL;
  1328. v*= 0x0101010101010101ULL;
  1329. for(y=0; y<block_height; y++){
  1330. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= u;
  1331. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= v;
  1332. }
  1333. //segmentation
  1334. if(IS_8X8(mb_type) || IS_16X8(mb_type)){
  1335. *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1336. *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1337. }
  1338. if(IS_8X8(mb_type) || IS_8X16(mb_type)){
  1339. for(y=0; y<16; y++)
  1340. pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
  1341. }
  1342. if(IS_8X8(mb_type) && mv_sample_log2 >= 2){
  1343. int dm= 1 << (mv_sample_log2-2);
  1344. for(i=0; i<4; i++){
  1345. int sx= mb_x*16 + 8*(i&1);
  1346. int sy= mb_y*16 + 8*(i>>1);
  1347. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1348. //FIXME bidir
  1349. int32_t *mv = (int32_t*)&pict->motion_val[0][xy];
  1350. if(mv[0] != mv[dm] || mv[dm*mv_stride] != mv[dm*(mv_stride+1)])
  1351. for(y=0; y<8; y++)
  1352. pict->data[0][sx + 4 + (sy + y)*pict->linesize[0]]^= 0x80;
  1353. if(mv[0] != mv[dm*mv_stride] || mv[dm] != mv[dm*(mv_stride+1)])
  1354. *(uint64_t*)(pict->data[0] + sx + (sy + 4)*pict->linesize[0])^= 0x8080808080808080ULL;
  1355. }
  1356. }
  1357. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
  1358. // hmm
  1359. }
  1360. }
  1361. s->mbskip_table[mb_index]=0;
  1362. }
  1363. }
  1364. }
  1365. }
  1366. static inline int hpel_motion_lowres(MpegEncContext *s,
  1367. uint8_t *dest, uint8_t *src,
  1368. int field_based, int field_select,
  1369. int src_x, int src_y,
  1370. int width, int height, int stride,
  1371. int h_edge_pos, int v_edge_pos,
  1372. int w, int h, h264_chroma_mc_func *pix_op,
  1373. int motion_x, int motion_y)
  1374. {
  1375. const int lowres= s->avctx->lowres;
  1376. const int op_index= FFMIN(lowres, 2);
  1377. const int s_mask= (2<<lowres)-1;
  1378. int emu=0;
  1379. int sx, sy;
  1380. if(s->quarter_sample){
  1381. motion_x/=2;
  1382. motion_y/=2;
  1383. }
  1384. sx= motion_x & s_mask;
  1385. sy= motion_y & s_mask;
  1386. src_x += motion_x >> (lowres+1);
  1387. src_y += motion_y >> (lowres+1);
  1388. src += src_y * stride + src_x;
  1389. if( (unsigned)src_x > h_edge_pos - (!!sx) - w
  1390. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1391. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
  1392. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1393. src= s->edge_emu_buffer;
  1394. emu=1;
  1395. }
  1396. sx= (sx << 2) >> lowres;
  1397. sy= (sy << 2) >> lowres;
  1398. if(field_select)
  1399. src += s->linesize;
  1400. pix_op[op_index](dest, src, stride, h, sx, sy);
  1401. return emu;
  1402. }
  1403. /* apply one mpeg motion vector to the three components */
  1404. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1405. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1406. int field_based, int bottom_field, int field_select,
  1407. uint8_t **ref_picture, h264_chroma_mc_func *pix_op,
  1408. int motion_x, int motion_y, int h, int mb_y)
  1409. {
  1410. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1411. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy, uvsx, uvsy;
  1412. const int lowres= s->avctx->lowres;
  1413. const int op_index= FFMIN(lowres-1+s->chroma_x_shift, 2);
  1414. const int block_s= 8>>lowres;
  1415. const int s_mask= (2<<lowres)-1;
  1416. const int h_edge_pos = s->h_edge_pos >> lowres;
  1417. const int v_edge_pos = s->v_edge_pos >> lowres;
  1418. linesize = s->current_picture.linesize[0] << field_based;
  1419. uvlinesize = s->current_picture.linesize[1] << field_based;
  1420. if(s->quarter_sample){ //FIXME obviously not perfect but qpel will not work in lowres anyway
  1421. motion_x/=2;
  1422. motion_y/=2;
  1423. }
  1424. if(field_based){
  1425. motion_y += (bottom_field - field_select)*((1<<lowres)-1);
  1426. }
  1427. sx= motion_x & s_mask;
  1428. sy= motion_y & s_mask;
  1429. src_x = s->mb_x*2*block_s + (motion_x >> (lowres+1));
  1430. src_y =( mb_y*2*block_s>>field_based) + (motion_y >> (lowres+1));
  1431. if (s->out_format == FMT_H263) {
  1432. uvsx = ((motion_x>>1) & s_mask) | (sx&1);
  1433. uvsy = ((motion_y>>1) & s_mask) | (sy&1);
  1434. uvsrc_x = src_x>>1;
  1435. uvsrc_y = src_y>>1;
  1436. }else if(s->out_format == FMT_H261){//even chroma mv's are full pel in H261
  1437. mx = motion_x / 4;
  1438. my = motion_y / 4;
  1439. uvsx = (2*mx) & s_mask;
  1440. uvsy = (2*my) & s_mask;
  1441. uvsrc_x = s->mb_x*block_s + (mx >> lowres);
  1442. uvsrc_y = mb_y*block_s + (my >> lowres);
  1443. } else {
  1444. if(s->chroma_y_shift){
  1445. mx = motion_x / 2;
  1446. my = motion_y / 2;
  1447. uvsx = mx & s_mask;
  1448. uvsy = my & s_mask;
  1449. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1450. uvsrc_y =( mb_y*block_s>>field_based) + (my >> (lowres+1));
  1451. } else {
  1452. if(s->chroma_x_shift){
  1453. //Chroma422
  1454. mx = motion_x / 2;
  1455. uvsx = mx & s_mask;
  1456. uvsy = motion_y & s_mask;
  1457. uvsrc_y = src_y;
  1458. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1459. } else {
  1460. //Chroma444
  1461. uvsx = motion_x & s_mask;
  1462. uvsy = motion_y & s_mask;
  1463. uvsrc_x = src_x;
  1464. uvsrc_y = src_y;
  1465. }
  1466. }
  1467. }
  1468. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1469. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1470. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1471. if( (unsigned)src_x > h_edge_pos - (!!sx) - 2*block_s
  1472. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1473. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
  1474. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1475. ptr_y = s->edge_emu_buffer;
  1476. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1477. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  1478. s->dsp.emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
  1479. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1480. s->dsp.emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
  1481. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1482. ptr_cb= uvbuf;
  1483. ptr_cr= uvbuf+16;
  1484. }
  1485. }
  1486. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.data
  1487. dest_y += s->linesize;
  1488. dest_cb+= s->uvlinesize;
  1489. dest_cr+= s->uvlinesize;
  1490. }
  1491. if(field_select){
  1492. ptr_y += s->linesize;
  1493. ptr_cb+= s->uvlinesize;
  1494. ptr_cr+= s->uvlinesize;
  1495. }
  1496. sx= (sx << 2) >> lowres;
  1497. sy= (sy << 2) >> lowres;
  1498. pix_op[lowres-1](dest_y, ptr_y, linesize, h, sx, sy);
  1499. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1500. uvsx= (uvsx << 2) >> lowres;
  1501. uvsy= (uvsy << 2) >> lowres;
  1502. if(h >> s->chroma_y_shift){
  1503. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1504. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1505. }
  1506. }
  1507. //FIXME h261 lowres loop filter
  1508. }
  1509. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1510. uint8_t *dest_cb, uint8_t *dest_cr,
  1511. uint8_t **ref_picture,
  1512. h264_chroma_mc_func *pix_op,
  1513. int mx, int my){
  1514. const int lowres= s->avctx->lowres;
  1515. const int op_index= FFMIN(lowres, 2);
  1516. const int block_s= 8>>lowres;
  1517. const int s_mask= (2<<lowres)-1;
  1518. const int h_edge_pos = s->h_edge_pos >> (lowres+1);
  1519. const int v_edge_pos = s->v_edge_pos >> (lowres+1);
  1520. int emu=0, src_x, src_y, offset, sx, sy;
  1521. uint8_t *ptr;
  1522. if(s->quarter_sample){
  1523. mx/=2;
  1524. my/=2;
  1525. }
  1526. /* In case of 8X8, we construct a single chroma motion vector
  1527. with a special rounding */
  1528. mx= ff_h263_round_chroma(mx);
  1529. my= ff_h263_round_chroma(my);
  1530. sx= mx & s_mask;
  1531. sy= my & s_mask;
  1532. src_x = s->mb_x*block_s + (mx >> (lowres+1));
  1533. src_y = s->mb_y*block_s + (my >> (lowres+1));
  1534. offset = src_y * s->uvlinesize + src_x;
  1535. ptr = ref_picture[1] + offset;
  1536. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1537. if( (unsigned)src_x > h_edge_pos - (!!sx) - block_s
  1538. || (unsigned)src_y > v_edge_pos - (!!sy) - block_s){
  1539. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1540. ptr= s->edge_emu_buffer;
  1541. emu=1;
  1542. }
  1543. }
  1544. sx= (sx << 2) >> lowres;
  1545. sy= (sy << 2) >> lowres;
  1546. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1547. ptr = ref_picture[2] + offset;
  1548. if(emu){
  1549. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1550. ptr= s->edge_emu_buffer;
  1551. }
  1552. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  1553. }
  1554. /**
  1555. * motion compensation of a single macroblock
  1556. * @param s context
  1557. * @param dest_y luma destination pointer
  1558. * @param dest_cb chroma cb/u destination pointer
  1559. * @param dest_cr chroma cr/v destination pointer
  1560. * @param dir direction (0->forward, 1->backward)
  1561. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1562. * @param pix_op halfpel motion compensation function (average or put normally)
  1563. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1564. */
  1565. static inline void MPV_motion_lowres(MpegEncContext *s,
  1566. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1567. int dir, uint8_t **ref_picture,
  1568. h264_chroma_mc_func *pix_op)
  1569. {
  1570. int mx, my;
  1571. int mb_x, mb_y, i;
  1572. const int lowres= s->avctx->lowres;
  1573. const int block_s= 8>>lowres;
  1574. mb_x = s->mb_x;
  1575. mb_y = s->mb_y;
  1576. switch(s->mv_type) {
  1577. case MV_TYPE_16X16:
  1578. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1579. 0, 0, 0,
  1580. ref_picture, pix_op,
  1581. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s, mb_y);
  1582. break;
  1583. case MV_TYPE_8X8:
  1584. mx = 0;
  1585. my = 0;
  1586. for(i=0;i<4;i++) {
  1587. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) * s->linesize)*block_s,
  1588. ref_picture[0], 0, 0,
  1589. (2*mb_x + (i & 1))*block_s, (2*mb_y + (i >>1))*block_s,
  1590. s->width, s->height, s->linesize,
  1591. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  1592. block_s, block_s, pix_op,
  1593. s->mv[dir][i][0], s->mv[dir][i][1]);
  1594. mx += s->mv[dir][i][0];
  1595. my += s->mv[dir][i][1];
  1596. }
  1597. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
  1598. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, pix_op, mx, my);
  1599. break;
  1600. case MV_TYPE_FIELD:
  1601. if (s->picture_structure == PICT_FRAME) {
  1602. /* top field */
  1603. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1604. 1, 0, s->field_select[dir][0],
  1605. ref_picture, pix_op,
  1606. s->mv[dir][0][0], s->mv[dir][0][1], block_s, mb_y);
  1607. /* bottom field */
  1608. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1609. 1, 1, s->field_select[dir][1],
  1610. ref_picture, pix_op,
  1611. s->mv[dir][1][0], s->mv[dir][1][1], block_s, mb_y);
  1612. } else {
  1613. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field){
  1614. ref_picture= s->current_picture_ptr->data;
  1615. }
  1616. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1617. 0, 0, s->field_select[dir][0],
  1618. ref_picture, pix_op,
  1619. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s, mb_y>>1);
  1620. }
  1621. break;
  1622. case MV_TYPE_16X8:
  1623. for(i=0; i<2; i++){
  1624. uint8_t ** ref2picture;
  1625. if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == AV_PICTURE_TYPE_B || s->first_field){
  1626. ref2picture= ref_picture;
  1627. }else{
  1628. ref2picture= s->current_picture_ptr->data;
  1629. }
  1630. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1631. 0, 0, s->field_select[dir][i],
  1632. ref2picture, pix_op,
  1633. s->mv[dir][i][0], s->mv[dir][i][1] + 2*block_s*i, block_s, mb_y>>1);
  1634. dest_y += 2*block_s*s->linesize;
  1635. dest_cb+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1636. dest_cr+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1637. }
  1638. break;
  1639. case MV_TYPE_DMV:
  1640. if(s->picture_structure == PICT_FRAME){
  1641. for(i=0; i<2; i++){
  1642. int j;
  1643. for(j=0; j<2; j++){
  1644. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1645. 1, j, j^i,
  1646. ref_picture, pix_op,
  1647. s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], block_s, mb_y);
  1648. }
  1649. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1650. }
  1651. }else{
  1652. for(i=0; i<2; i++){
  1653. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1654. 0, 0, s->picture_structure != i+1,
  1655. ref_picture, pix_op,
  1656. s->mv[dir][2*i][0],s->mv[dir][2*i][1],2*block_s, mb_y>>1);
  1657. // after put we make avg of the same block
  1658. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1659. //opposite parity is always in the same frame if this is second field
  1660. if(!s->first_field){
  1661. ref_picture = s->current_picture_ptr->data;
  1662. }
  1663. }
  1664. }
  1665. break;
  1666. default: assert(0);
  1667. }
  1668. }
  1669. /**
  1670. * find the lowest MB row referenced in the MVs
  1671. */
  1672. int MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  1673. {
  1674. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1675. int my, off, i, mvs;
  1676. if (s->picture_structure != PICT_FRAME) goto unhandled;
  1677. switch (s->mv_type) {
  1678. case MV_TYPE_16X16:
  1679. mvs = 1;
  1680. break;
  1681. case MV_TYPE_16X8:
  1682. mvs = 2;
  1683. break;
  1684. case MV_TYPE_8X8:
  1685. mvs = 4;
  1686. break;
  1687. default:
  1688. goto unhandled;
  1689. }
  1690. for (i = 0; i < mvs; i++) {
  1691. my = s->mv[dir][i][1]<<qpel_shift;
  1692. my_max = FFMAX(my_max, my);
  1693. my_min = FFMIN(my_min, my);
  1694. }
  1695. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  1696. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  1697. unhandled:
  1698. return s->mb_height-1;
  1699. }
  1700. /* put block[] to dest[] */
  1701. static inline void put_dct(MpegEncContext *s,
  1702. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1703. {
  1704. s->dct_unquantize_intra(s, block, i, qscale);
  1705. s->dsp.idct_put (dest, line_size, block);
  1706. }
  1707. /* add block[] to dest[] */
  1708. static inline void add_dct(MpegEncContext *s,
  1709. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1710. {
  1711. if (s->block_last_index[i] >= 0) {
  1712. s->dsp.idct_add (dest, line_size, block);
  1713. }
  1714. }
  1715. static inline void add_dequant_dct(MpegEncContext *s,
  1716. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1717. {
  1718. if (s->block_last_index[i] >= 0) {
  1719. s->dct_unquantize_inter(s, block, i, qscale);
  1720. s->dsp.idct_add (dest, line_size, block);
  1721. }
  1722. }
  1723. /**
  1724. * cleans dc, ac, coded_block for the current non intra MB
  1725. */
  1726. void ff_clean_intra_table_entries(MpegEncContext *s)
  1727. {
  1728. int wrap = s->b8_stride;
  1729. int xy = s->block_index[0];
  1730. s->dc_val[0][xy ] =
  1731. s->dc_val[0][xy + 1 ] =
  1732. s->dc_val[0][xy + wrap] =
  1733. s->dc_val[0][xy + 1 + wrap] = 1024;
  1734. /* ac pred */
  1735. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1736. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1737. if (s->msmpeg4_version>=3) {
  1738. s->coded_block[xy ] =
  1739. s->coded_block[xy + 1 ] =
  1740. s->coded_block[xy + wrap] =
  1741. s->coded_block[xy + 1 + wrap] = 0;
  1742. }
  1743. /* chroma */
  1744. wrap = s->mb_stride;
  1745. xy = s->mb_x + s->mb_y * wrap;
  1746. s->dc_val[1][xy] =
  1747. s->dc_val[2][xy] = 1024;
  1748. /* ac pred */
  1749. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1750. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1751. s->mbintra_table[xy]= 0;
  1752. }
  1753. /* generic function called after a macroblock has been parsed by the
  1754. decoder or after it has been encoded by the encoder.
  1755. Important variables used:
  1756. s->mb_intra : true if intra macroblock
  1757. s->mv_dir : motion vector direction
  1758. s->mv_type : motion vector type
  1759. s->mv : motion vector
  1760. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1761. */
  1762. static av_always_inline
  1763. void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64],
  1764. int lowres_flag, int is_mpeg12)
  1765. {
  1766. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1767. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1768. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1769. return;
  1770. }
  1771. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1772. /* save DCT coefficients */
  1773. int i,j;
  1774. DCTELEM *dct = &s->current_picture.dct_coeff[mb_xy*64*6];
  1775. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1776. for(i=0; i<6; i++){
  1777. for(j=0; j<64; j++){
  1778. *dct++ = block[i][s->dsp.idct_permutation[j]];
  1779. av_log(s->avctx, AV_LOG_DEBUG, "%5d", dct[-1]);
  1780. }
  1781. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1782. }
  1783. }
  1784. s->current_picture.qscale_table[mb_xy]= s->qscale;
  1785. /* update DC predictors for P macroblocks */
  1786. if (!s->mb_intra) {
  1787. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1788. if(s->mbintra_table[mb_xy])
  1789. ff_clean_intra_table_entries(s);
  1790. } else {
  1791. s->last_dc[0] =
  1792. s->last_dc[1] =
  1793. s->last_dc[2] = 128 << s->intra_dc_precision;
  1794. }
  1795. }
  1796. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1797. s->mbintra_table[mb_xy]=1;
  1798. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
  1799. uint8_t *dest_y, *dest_cb, *dest_cr;
  1800. int dct_linesize, dct_offset;
  1801. op_pixels_func (*op_pix)[4];
  1802. qpel_mc_func (*op_qpix)[16];
  1803. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics
  1804. const int uvlinesize= s->current_picture.linesize[1];
  1805. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  1806. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  1807. /* avoid copy if macroblock skipped in last frame too */
  1808. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1809. if(!s->encoding){
  1810. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1811. const int age= s->current_picture.age;
  1812. assert(age);
  1813. if (s->mb_skipped) {
  1814. s->mb_skipped= 0;
  1815. assert(s->pict_type!=AV_PICTURE_TYPE_I);
  1816. (*mbskip_ptr) ++; /* indicate that this time we skipped it */
  1817. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1818. /* if previous was skipped too, then nothing to do ! */
  1819. if (*mbskip_ptr >= age && s->current_picture.reference){
  1820. return;
  1821. }
  1822. } else if(!s->current_picture.reference){
  1823. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  1824. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1825. } else{
  1826. *mbskip_ptr = 0; /* not skipped */
  1827. }
  1828. }
  1829. dct_linesize = linesize << s->interlaced_dct;
  1830. dct_offset =(s->interlaced_dct)? linesize : linesize*block_size;
  1831. if(readable){
  1832. dest_y= s->dest[0];
  1833. dest_cb= s->dest[1];
  1834. dest_cr= s->dest[2];
  1835. }else{
  1836. dest_y = s->b_scratchpad;
  1837. dest_cb= s->b_scratchpad+16*linesize;
  1838. dest_cr= s->b_scratchpad+32*linesize;
  1839. }
  1840. if (!s->mb_intra) {
  1841. /* motion handling */
  1842. /* decoding or more than one mb_type (MC was already done otherwise) */
  1843. if(!s->encoding){
  1844. if(HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1845. if (s->mv_dir & MV_DIR_FORWARD) {
  1846. ff_thread_await_progress((AVFrame*)s->last_picture_ptr, MPV_lowest_referenced_row(s, 0), 0);
  1847. }
  1848. if (s->mv_dir & MV_DIR_BACKWARD) {
  1849. ff_thread_await_progress((AVFrame*)s->next_picture_ptr, MPV_lowest_referenced_row(s, 1), 0);
  1850. }
  1851. }
  1852. if(lowres_flag){
  1853. h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
  1854. if (s->mv_dir & MV_DIR_FORWARD) {
  1855. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix);
  1856. op_pix = s->dsp.avg_h264_chroma_pixels_tab;
  1857. }
  1858. if (s->mv_dir & MV_DIR_BACKWARD) {
  1859. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix);
  1860. }
  1861. }else{
  1862. op_qpix= s->me.qpel_put;
  1863. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1864. op_pix = s->dsp.put_pixels_tab;
  1865. }else{
  1866. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1867. }
  1868. if (s->mv_dir & MV_DIR_FORWARD) {
  1869. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  1870. op_pix = s->dsp.avg_pixels_tab;
  1871. op_qpix= s->me.qpel_avg;
  1872. }
  1873. if (s->mv_dir & MV_DIR_BACKWARD) {
  1874. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  1875. }
  1876. }
  1877. }
  1878. /* skip dequant / idct if we are really late ;) */
  1879. if(s->avctx->skip_idct){
  1880. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  1881. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  1882. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1883. goto skip_idct;
  1884. }
  1885. /* add dct residue */
  1886. if(s->encoding || !( s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  1887. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1888. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1889. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1890. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1891. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1892. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1893. if (s->chroma_y_shift){
  1894. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1895. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1896. }else{
  1897. dct_linesize >>= 1;
  1898. dct_offset >>=1;
  1899. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1900. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1901. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1902. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1903. }
  1904. }
  1905. } else if(is_mpeg12 || (s->codec_id != CODEC_ID_WMV2)){
  1906. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1907. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1908. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1909. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1910. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1911. if(s->chroma_y_shift){//Chroma420
  1912. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1913. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1914. }else{
  1915. //chroma422
  1916. dct_linesize = uvlinesize << s->interlaced_dct;
  1917. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*block_size;
  1918. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1919. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1920. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1921. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1922. if(!s->chroma_x_shift){//Chroma444
  1923. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  1924. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  1925. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  1926. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  1927. }
  1928. }
  1929. }//fi gray
  1930. }
  1931. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  1932. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1933. }
  1934. } else {
  1935. /* dct only in intra block */
  1936. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  1937. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1938. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1939. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1940. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1941. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1942. if(s->chroma_y_shift){
  1943. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1944. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1945. }else{
  1946. dct_offset >>=1;
  1947. dct_linesize >>=1;
  1948. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1949. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1950. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1951. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1952. }
  1953. }
  1954. }else{
  1955. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  1956. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1957. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1958. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1959. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1960. if(s->chroma_y_shift){
  1961. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  1962. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  1963. }else{
  1964. dct_linesize = uvlinesize << s->interlaced_dct;
  1965. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*block_size;
  1966. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  1967. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  1968. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1969. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1970. if(!s->chroma_x_shift){//Chroma444
  1971. s->dsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  1972. s->dsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  1973. s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  1974. s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  1975. }
  1976. }
  1977. }//gray
  1978. }
  1979. }
  1980. skip_idct:
  1981. if(!readable){
  1982. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  1983. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  1984. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  1985. }
  1986. }
  1987. }
  1988. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
  1989. #if !CONFIG_SMALL
  1990. if(s->out_format == FMT_MPEG1) {
  1991. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  1992. else MPV_decode_mb_internal(s, block, 0, 1);
  1993. } else
  1994. #endif
  1995. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  1996. else MPV_decode_mb_internal(s, block, 0, 0);
  1997. }
  1998. /**
  1999. *
  2000. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2001. */
  2002. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  2003. const int field_pic= s->picture_structure != PICT_FRAME;
  2004. if(field_pic){
  2005. h <<= 1;
  2006. y <<= 1;
  2007. }
  2008. if (!s->avctx->hwaccel
  2009. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2010. && s->unrestricted_mv
  2011. && s->current_picture.reference
  2012. && !s->intra_only
  2013. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  2014. int sides = 0, edge_h;
  2015. if (y==0) sides |= EDGE_TOP;
  2016. if (y + h >= s->v_edge_pos) sides |= EDGE_BOTTOM;
  2017. edge_h= FFMIN(h, s->v_edge_pos - y);
  2018. s->dsp.draw_edges(s->current_picture_ptr->data[0] + y *s->linesize , s->linesize , s->h_edge_pos , edge_h , EDGE_WIDTH , sides);
  2019. s->dsp.draw_edges(s->current_picture_ptr->data[1] + (y>>1)*s->uvlinesize, s->uvlinesize, s->h_edge_pos>>1, edge_h>>1, EDGE_WIDTH/2, sides);
  2020. s->dsp.draw_edges(s->current_picture_ptr->data[2] + (y>>1)*s->uvlinesize, s->uvlinesize, s->h_edge_pos>>1, edge_h>>1, EDGE_WIDTH/2, sides);
  2021. }
  2022. h= FFMIN(h, s->avctx->height - y);
  2023. if(field_pic && s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2024. if (s->avctx->draw_horiz_band) {
  2025. AVFrame *src;
  2026. int offset[4];
  2027. if(s->pict_type==AV_PICTURE_TYPE_B || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  2028. src= (AVFrame*)s->current_picture_ptr;
  2029. else if(s->last_picture_ptr)
  2030. src= (AVFrame*)s->last_picture_ptr;
  2031. else
  2032. return;
  2033. if(s->pict_type==AV_PICTURE_TYPE_B && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  2034. offset[0]=
  2035. offset[1]=
  2036. offset[2]=
  2037. offset[3]= 0;
  2038. }else{
  2039. offset[0]= y * s->linesize;
  2040. offset[1]=
  2041. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  2042. offset[3]= 0;
  2043. }
  2044. emms_c();
  2045. s->avctx->draw_horiz_band(s->avctx, src, offset,
  2046. y, s->picture_structure, h);
  2047. }
  2048. }
  2049. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2050. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this would be wrong for field pics
  2051. const int uvlinesize= s->current_picture.linesize[1];
  2052. const int mb_size= 4 - s->avctx->lowres;
  2053. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2054. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2055. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2056. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2057. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2058. s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2059. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2060. s->dest[0] = s->current_picture.data[0] + ((s->mb_x - 1) << mb_size);
  2061. s->dest[1] = s->current_picture.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2062. s->dest[2] = s->current_picture.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2063. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2064. {
  2065. if(s->picture_structure==PICT_FRAME){
  2066. s->dest[0] += s->mb_y * linesize << mb_size;
  2067. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2068. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2069. }else{
  2070. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2071. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2072. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2073. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2074. }
  2075. }
  2076. }
  2077. void ff_mpeg_flush(AVCodecContext *avctx){
  2078. int i;
  2079. MpegEncContext *s = avctx->priv_data;
  2080. if(s==NULL || s->picture==NULL)
  2081. return;
  2082. for(i=0; i<s->picture_count; i++){
  2083. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  2084. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  2085. free_frame_buffer(s, &s->picture[i]);
  2086. }
  2087. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2088. s->mb_x= s->mb_y= 0;
  2089. s->closed_gop= 0;
  2090. s->parse_context.state= -1;
  2091. s->parse_context.frame_start_found= 0;
  2092. s->parse_context.overread= 0;
  2093. s->parse_context.overread_index= 0;
  2094. s->parse_context.index= 0;
  2095. s->parse_context.last_index= 0;
  2096. s->bitstream_buffer_size=0;
  2097. s->pp_time=0;
  2098. }
  2099. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  2100. DCTELEM *block, int n, int qscale)
  2101. {
  2102. int i, level, nCoeffs;
  2103. const uint16_t *quant_matrix;
  2104. nCoeffs= s->block_last_index[n];
  2105. if (n < 4)
  2106. block[0] = block[0] * s->y_dc_scale;
  2107. else
  2108. block[0] = block[0] * s->c_dc_scale;
  2109. /* XXX: only mpeg1 */
  2110. quant_matrix = s->intra_matrix;
  2111. for(i=1;i<=nCoeffs;i++) {
  2112. int j= s->intra_scantable.permutated[i];
  2113. level = block[j];
  2114. if (level) {
  2115. if (level < 0) {
  2116. level = -level;
  2117. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2118. level = (level - 1) | 1;
  2119. level = -level;
  2120. } else {
  2121. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2122. level = (level - 1) | 1;
  2123. }
  2124. block[j] = level;
  2125. }
  2126. }
  2127. }
  2128. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  2129. DCTELEM *block, int n, int qscale)
  2130. {
  2131. int i, level, nCoeffs;
  2132. const uint16_t *quant_matrix;
  2133. nCoeffs= s->block_last_index[n];
  2134. quant_matrix = s->inter_matrix;
  2135. for(i=0; i<=nCoeffs; i++) {
  2136. int j= s->intra_scantable.permutated[i];
  2137. level = block[j];
  2138. if (level) {
  2139. if (level < 0) {
  2140. level = -level;
  2141. level = (((level << 1) + 1) * qscale *
  2142. ((int) (quant_matrix[j]))) >> 4;
  2143. level = (level - 1) | 1;
  2144. level = -level;
  2145. } else {
  2146. level = (((level << 1) + 1) * qscale *
  2147. ((int) (quant_matrix[j]))) >> 4;
  2148. level = (level - 1) | 1;
  2149. }
  2150. block[j] = level;
  2151. }
  2152. }
  2153. }
  2154. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2155. DCTELEM *block, int n, int qscale)
  2156. {
  2157. int i, level, nCoeffs;
  2158. const uint16_t *quant_matrix;
  2159. if(s->alternate_scan) nCoeffs= 63;
  2160. else nCoeffs= s->block_last_index[n];
  2161. if (n < 4)
  2162. block[0] = block[0] * s->y_dc_scale;
  2163. else
  2164. block[0] = block[0] * s->c_dc_scale;
  2165. quant_matrix = s->intra_matrix;
  2166. for(i=1;i<=nCoeffs;i++) {
  2167. int j= s->intra_scantable.permutated[i];
  2168. level = block[j];
  2169. if (level) {
  2170. if (level < 0) {
  2171. level = -level;
  2172. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2173. level = -level;
  2174. } else {
  2175. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2176. }
  2177. block[j] = level;
  2178. }
  2179. }
  2180. }
  2181. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2182. DCTELEM *block, int n, int qscale)
  2183. {
  2184. int i, level, nCoeffs;
  2185. const uint16_t *quant_matrix;
  2186. int sum=-1;
  2187. if(s->alternate_scan) nCoeffs= 63;
  2188. else nCoeffs= s->block_last_index[n];
  2189. if (n < 4)
  2190. block[0] = block[0] * s->y_dc_scale;
  2191. else
  2192. block[0] = block[0] * s->c_dc_scale;
  2193. quant_matrix = s->intra_matrix;
  2194. for(i=1;i<=nCoeffs;i++) {
  2195. int j= s->intra_scantable.permutated[i];
  2196. level = block[j];
  2197. if (level) {
  2198. if (level < 0) {
  2199. level = -level;
  2200. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2201. level = -level;
  2202. } else {
  2203. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2204. }
  2205. block[j] = level;
  2206. sum+=level;
  2207. }
  2208. }
  2209. block[63]^=sum&1;
  2210. }
  2211. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2212. DCTELEM *block, int n, int qscale)
  2213. {
  2214. int i, level, nCoeffs;
  2215. const uint16_t *quant_matrix;
  2216. int sum=-1;
  2217. if(s->alternate_scan) nCoeffs= 63;
  2218. else nCoeffs= s->block_last_index[n];
  2219. quant_matrix = s->inter_matrix;
  2220. for(i=0; i<=nCoeffs; i++) {
  2221. int j= s->intra_scantable.permutated[i];
  2222. level = block[j];
  2223. if (level) {
  2224. if (level < 0) {
  2225. level = -level;
  2226. level = (((level << 1) + 1) * qscale *
  2227. ((int) (quant_matrix[j]))) >> 4;
  2228. level = -level;
  2229. } else {
  2230. level = (((level << 1) + 1) * qscale *
  2231. ((int) (quant_matrix[j]))) >> 4;
  2232. }
  2233. block[j] = level;
  2234. sum+=level;
  2235. }
  2236. }
  2237. block[63]^=sum&1;
  2238. }
  2239. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2240. DCTELEM *block, int n, int qscale)
  2241. {
  2242. int i, level, qmul, qadd;
  2243. int nCoeffs;
  2244. assert(s->block_last_index[n]>=0);
  2245. qmul = qscale << 1;
  2246. if (!s->h263_aic) {
  2247. if (n < 4)
  2248. block[0] = block[0] * s->y_dc_scale;
  2249. else
  2250. block[0] = block[0] * s->c_dc_scale;
  2251. qadd = (qscale - 1) | 1;
  2252. }else{
  2253. qadd = 0;
  2254. }
  2255. if(s->ac_pred)
  2256. nCoeffs=63;
  2257. else
  2258. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2259. for(i=1; i<=nCoeffs; i++) {
  2260. level = block[i];
  2261. if (level) {
  2262. if (level < 0) {
  2263. level = level * qmul - qadd;
  2264. } else {
  2265. level = level * qmul + qadd;
  2266. }
  2267. block[i] = level;
  2268. }
  2269. }
  2270. }
  2271. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2272. DCTELEM *block, int n, int qscale)
  2273. {
  2274. int i, level, qmul, qadd;
  2275. int nCoeffs;
  2276. assert(s->block_last_index[n]>=0);
  2277. qadd = (qscale - 1) | 1;
  2278. qmul = qscale << 1;
  2279. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2280. for(i=0; i<=nCoeffs; i++) {
  2281. level = block[i];
  2282. if (level) {
  2283. if (level < 0) {
  2284. level = level * qmul - qadd;
  2285. } else {
  2286. level = level * qmul + qadd;
  2287. }
  2288. block[i] = level;
  2289. }
  2290. }
  2291. }
  2292. /**
  2293. * set qscale and update qscale dependent variables.
  2294. */
  2295. void ff_set_qscale(MpegEncContext * s, int qscale)
  2296. {
  2297. if (qscale < 1)
  2298. qscale = 1;
  2299. else if (qscale > 31)
  2300. qscale = 31;
  2301. s->qscale = qscale;
  2302. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2303. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2304. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2305. }
  2306. void MPV_report_decode_progress(MpegEncContext *s)
  2307. {
  2308. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame)
  2309. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_y, 0);
  2310. }