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.

3213 lines
115KB

  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/attributes.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/imgutils.h"
  31. #include "libavutil/internal.h"
  32. #include "libavutil/timer.h"
  33. #include "avcodec.h"
  34. #include "dsputil.h"
  35. #include "h264chroma.h"
  36. #include "internal.h"
  37. #include "mathops.h"
  38. #include "mpegvideo.h"
  39. #include "mjpegenc.h"
  40. #include "msmpeg4.h"
  41. #include "thread.h"
  42. #include <limits.h>
  43. static const uint8_t ff_default_chroma_qscale_table[32] = {
  44. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  45. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  46. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
  47. };
  48. const uint8_t ff_mpeg1_dc_scale_table[128] = {
  49. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  50. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  51. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  52. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  53. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  54. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  55. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  56. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  57. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  58. };
  59. static const uint8_t mpeg2_dc_scale_table1[128] = {
  60. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  61. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  62. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  63. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  64. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  65. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  66. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  67. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  68. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  69. };
  70. static const uint8_t mpeg2_dc_scale_table2[128] = {
  71. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  72. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  73. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  74. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  75. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  76. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  77. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  78. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  79. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  80. };
  81. static const uint8_t mpeg2_dc_scale_table3[128] = {
  82. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  83. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  84. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  85. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  86. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  87. 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,
  89. 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,
  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 AVPixelFormat ff_pixfmt_list_420[] = {
  99. AV_PIX_FMT_YUV420P,
  100. AV_PIX_FMT_NONE
  101. };
  102. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  103. int16_t *block, int n, int qscale)
  104. {
  105. int i, level, nCoeffs;
  106. const uint16_t *quant_matrix;
  107. nCoeffs= s->block_last_index[n];
  108. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  109. /* XXX: only mpeg1 */
  110. quant_matrix = s->intra_matrix;
  111. for(i=1;i<=nCoeffs;i++) {
  112. int j= s->intra_scantable.permutated[i];
  113. level = block[j];
  114. if (level) {
  115. if (level < 0) {
  116. level = -level;
  117. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  118. level = (level - 1) | 1;
  119. level = -level;
  120. } else {
  121. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  122. level = (level - 1) | 1;
  123. }
  124. block[j] = level;
  125. }
  126. }
  127. }
  128. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  129. int16_t *block, int n, int qscale)
  130. {
  131. int i, level, nCoeffs;
  132. const uint16_t *quant_matrix;
  133. nCoeffs= s->block_last_index[n];
  134. quant_matrix = s->inter_matrix;
  135. for(i=0; i<=nCoeffs; i++) {
  136. int j= s->intra_scantable.permutated[i];
  137. level = block[j];
  138. if (level) {
  139. if (level < 0) {
  140. level = -level;
  141. level = (((level << 1) + 1) * qscale *
  142. ((int) (quant_matrix[j]))) >> 4;
  143. level = (level - 1) | 1;
  144. level = -level;
  145. } else {
  146. level = (((level << 1) + 1) * qscale *
  147. ((int) (quant_matrix[j]))) >> 4;
  148. level = (level - 1) | 1;
  149. }
  150. block[j] = level;
  151. }
  152. }
  153. }
  154. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  155. int16_t *block, int n, int qscale)
  156. {
  157. int i, level, nCoeffs;
  158. const uint16_t *quant_matrix;
  159. if(s->alternate_scan) nCoeffs= 63;
  160. else nCoeffs= s->block_last_index[n];
  161. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  162. quant_matrix = s->intra_matrix;
  163. for(i=1;i<=nCoeffs;i++) {
  164. int j= s->intra_scantable.permutated[i];
  165. level = block[j];
  166. if (level) {
  167. if (level < 0) {
  168. level = -level;
  169. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  170. level = -level;
  171. } else {
  172. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  173. }
  174. block[j] = level;
  175. }
  176. }
  177. }
  178. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  179. int16_t *block, int n, int qscale)
  180. {
  181. int i, level, nCoeffs;
  182. const uint16_t *quant_matrix;
  183. int sum=-1;
  184. if(s->alternate_scan) nCoeffs= 63;
  185. else nCoeffs= s->block_last_index[n];
  186. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  187. sum += block[0];
  188. quant_matrix = s->intra_matrix;
  189. for(i=1;i<=nCoeffs;i++) {
  190. int j= s->intra_scantable.permutated[i];
  191. level = block[j];
  192. if (level) {
  193. if (level < 0) {
  194. level = -level;
  195. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  196. level = -level;
  197. } else {
  198. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  199. }
  200. block[j] = level;
  201. sum+=level;
  202. }
  203. }
  204. block[63]^=sum&1;
  205. }
  206. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  207. int16_t *block, int n, int qscale)
  208. {
  209. int i, level, nCoeffs;
  210. const uint16_t *quant_matrix;
  211. int sum=-1;
  212. if(s->alternate_scan) nCoeffs= 63;
  213. else nCoeffs= s->block_last_index[n];
  214. quant_matrix = s->inter_matrix;
  215. for(i=0; i<=nCoeffs; i++) {
  216. int j= s->intra_scantable.permutated[i];
  217. level = block[j];
  218. if (level) {
  219. if (level < 0) {
  220. level = -level;
  221. level = (((level << 1) + 1) * qscale *
  222. ((int) (quant_matrix[j]))) >> 4;
  223. level = -level;
  224. } else {
  225. level = (((level << 1) + 1) * qscale *
  226. ((int) (quant_matrix[j]))) >> 4;
  227. }
  228. block[j] = level;
  229. sum+=level;
  230. }
  231. }
  232. block[63]^=sum&1;
  233. }
  234. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  235. int16_t *block, int n, int qscale)
  236. {
  237. int i, level, qmul, qadd;
  238. int nCoeffs;
  239. av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
  240. qmul = qscale << 1;
  241. if (!s->h263_aic) {
  242. block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
  243. qadd = (qscale - 1) | 1;
  244. }else{
  245. qadd = 0;
  246. }
  247. if(s->ac_pred)
  248. nCoeffs=63;
  249. else
  250. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  251. for(i=1; i<=nCoeffs; i++) {
  252. level = block[i];
  253. if (level) {
  254. if (level < 0) {
  255. level = level * qmul - qadd;
  256. } else {
  257. level = level * qmul + qadd;
  258. }
  259. block[i] = level;
  260. }
  261. }
  262. }
  263. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  264. int16_t *block, int n, int qscale)
  265. {
  266. int i, level, qmul, qadd;
  267. int nCoeffs;
  268. av_assert2(s->block_last_index[n]>=0);
  269. qadd = (qscale - 1) | 1;
  270. qmul = qscale << 1;
  271. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  272. for(i=0; i<=nCoeffs; i++) {
  273. level = block[i];
  274. if (level) {
  275. if (level < 0) {
  276. level = level * qmul - qadd;
  277. } else {
  278. level = level * qmul + qadd;
  279. }
  280. block[i] = level;
  281. }
  282. }
  283. }
  284. static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
  285. int (*mv)[2][4][2],
  286. int mb_x, int mb_y, int mb_intra, int mb_skipped)
  287. {
  288. MpegEncContext *s = opaque;
  289. s->mv_dir = mv_dir;
  290. s->mv_type = mv_type;
  291. s->mb_intra = mb_intra;
  292. s->mb_skipped = mb_skipped;
  293. s->mb_x = mb_x;
  294. s->mb_y = mb_y;
  295. memcpy(s->mv, mv, sizeof(*mv));
  296. ff_init_block_index(s);
  297. ff_update_block_index(s);
  298. s->dsp.clear_blocks(s->block[0]);
  299. s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
  300. s->dest[1] = s->current_picture.f.data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
  301. s->dest[2] = s->current_picture.f.data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
  302. if (ref)
  303. av_log(s->avctx, AV_LOG_DEBUG, "Interlaced error concealment is not fully implemented\n");
  304. ff_MPV_decode_mb(s, s->block);
  305. }
  306. /* init common dct for both encoder and decoder */
  307. av_cold int ff_dct_common_init(MpegEncContext *s)
  308. {
  309. ff_dsputil_init(&s->dsp, s->avctx);
  310. ff_h264chroma_init(&s->h264chroma, 8); //for lowres
  311. ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
  312. ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
  313. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  314. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  315. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  316. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  317. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  318. if (s->flags & CODEC_FLAG_BITEXACT)
  319. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  320. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  321. if (ARCH_ALPHA)
  322. ff_MPV_common_init_axp(s);
  323. if (ARCH_ARM)
  324. ff_MPV_common_init_arm(s);
  325. if (ARCH_PPC)
  326. ff_MPV_common_init_ppc(s);
  327. if (ARCH_X86)
  328. ff_MPV_common_init_x86(s);
  329. /* load & permutate scantables
  330. * note: only wmv uses different ones
  331. */
  332. if (s->alternate_scan) {
  333. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  334. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  335. } else {
  336. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  337. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  338. }
  339. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  340. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  341. return 0;
  342. }
  343. static int frame_size_alloc(MpegEncContext *s, int linesize)
  344. {
  345. int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
  346. // edge emu needs blocksize + filter length - 1
  347. // (= 17x17 for halfpel / 21x21 for h264)
  348. // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
  349. // at uvlinesize. It supports only YUV420 so 24x24 is enough
  350. // linesize * interlaced * MBsize
  351. FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 4 * 24,
  352. fail);
  353. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 4 * 16 * 2,
  354. fail)
  355. s->me.temp = s->me.scratchpad;
  356. s->rd_scratchpad = s->me.scratchpad;
  357. s->b_scratchpad = s->me.scratchpad;
  358. s->obmc_scratchpad = s->me.scratchpad + 16;
  359. return 0;
  360. fail:
  361. av_freep(&s->edge_emu_buffer);
  362. return AVERROR(ENOMEM);
  363. }
  364. /**
  365. * Allocate a frame buffer
  366. */
  367. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  368. {
  369. int edges_needed = av_codec_is_encoder(s->avctx->codec);
  370. int r, ret;
  371. pic->tf.f = &pic->f;
  372. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  373. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  374. s->codec_id != AV_CODEC_ID_MSS2) {
  375. if (edges_needed) {
  376. pic->f.width = s->avctx->width + 2 * EDGE_WIDTH;
  377. pic->f.height = s->avctx->height + 2 * EDGE_WIDTH;
  378. }
  379. r = ff_thread_get_buffer(s->avctx, &pic->tf,
  380. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  381. } else {
  382. pic->f.width = s->avctx->width;
  383. pic->f.height = s->avctx->height;
  384. pic->f.format = s->avctx->pix_fmt;
  385. r = avcodec_default_get_buffer2(s->avctx, &pic->f, 0);
  386. }
  387. if (r < 0 || !pic->f.buf[0]) {
  388. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
  389. r, pic->f.data[0]);
  390. return -1;
  391. }
  392. if (edges_needed) {
  393. int i;
  394. for (i = 0; pic->f.data[i]; i++) {
  395. int offset = (EDGE_WIDTH >> (i ? s->chroma_y_shift : 0)) *
  396. pic->f.linesize[i] +
  397. (EDGE_WIDTH >> (i ? s->chroma_x_shift : 0));
  398. pic->f.data[i] += offset;
  399. }
  400. pic->f.width = s->avctx->width;
  401. pic->f.height = s->avctx->height;
  402. }
  403. if (s->avctx->hwaccel) {
  404. assert(!pic->hwaccel_picture_private);
  405. if (s->avctx->hwaccel->priv_data_size) {
  406. pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->priv_data_size);
  407. if (!pic->hwaccel_priv_buf) {
  408. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  409. return -1;
  410. }
  411. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  412. }
  413. }
  414. if (s->linesize && (s->linesize != pic->f.linesize[0] ||
  415. s->uvlinesize != pic->f.linesize[1])) {
  416. av_log(s->avctx, AV_LOG_ERROR,
  417. "get_buffer() failed (stride changed)\n");
  418. ff_mpeg_unref_picture(s, pic);
  419. return -1;
  420. }
  421. if (pic->f.linesize[1] != pic->f.linesize[2]) {
  422. av_log(s->avctx, AV_LOG_ERROR,
  423. "get_buffer() failed (uv stride mismatch)\n");
  424. ff_mpeg_unref_picture(s, pic);
  425. return -1;
  426. }
  427. if (!s->edge_emu_buffer &&
  428. (ret = frame_size_alloc(s, pic->f.linesize[0])) < 0) {
  429. av_log(s->avctx, AV_LOG_ERROR,
  430. "get_buffer() failed to allocate context scratch buffers.\n");
  431. ff_mpeg_unref_picture(s, pic);
  432. return ret;
  433. }
  434. return 0;
  435. }
  436. void ff_free_picture_tables(Picture *pic)
  437. {
  438. int i;
  439. pic->alloc_mb_width =
  440. pic->alloc_mb_height = 0;
  441. av_buffer_unref(&pic->mb_var_buf);
  442. av_buffer_unref(&pic->mc_mb_var_buf);
  443. av_buffer_unref(&pic->mb_mean_buf);
  444. av_buffer_unref(&pic->mbskip_table_buf);
  445. av_buffer_unref(&pic->qscale_table_buf);
  446. av_buffer_unref(&pic->mb_type_buf);
  447. for (i = 0; i < 2; i++) {
  448. av_buffer_unref(&pic->motion_val_buf[i]);
  449. av_buffer_unref(&pic->ref_index_buf[i]);
  450. }
  451. }
  452. static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
  453. {
  454. const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
  455. const int mb_array_size = s->mb_stride * s->mb_height;
  456. const int b8_array_size = s->b8_stride * s->mb_height * 2;
  457. int i;
  458. pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
  459. pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
  460. pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) *
  461. sizeof(uint32_t));
  462. if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
  463. return AVERROR(ENOMEM);
  464. if (s->encoding) {
  465. pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  466. pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  467. pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
  468. if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
  469. return AVERROR(ENOMEM);
  470. }
  471. if (s->out_format == FMT_H263 || s->encoding || s->avctx->debug_mv) {
  472. int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
  473. int ref_index_size = 4 * mb_array_size;
  474. for (i = 0; mv_size && i < 2; i++) {
  475. pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
  476. pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
  477. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  478. return AVERROR(ENOMEM);
  479. }
  480. }
  481. pic->alloc_mb_width = s->mb_width;
  482. pic->alloc_mb_height = s->mb_height;
  483. return 0;
  484. }
  485. static int make_tables_writable(Picture *pic)
  486. {
  487. int ret, i;
  488. #define MAKE_WRITABLE(table) \
  489. do {\
  490. if (pic->table &&\
  491. (ret = av_buffer_make_writable(&pic->table)) < 0)\
  492. return ret;\
  493. } while (0)
  494. MAKE_WRITABLE(mb_var_buf);
  495. MAKE_WRITABLE(mc_mb_var_buf);
  496. MAKE_WRITABLE(mb_mean_buf);
  497. MAKE_WRITABLE(mbskip_table_buf);
  498. MAKE_WRITABLE(qscale_table_buf);
  499. MAKE_WRITABLE(mb_type_buf);
  500. for (i = 0; i < 2; i++) {
  501. MAKE_WRITABLE(motion_val_buf[i]);
  502. MAKE_WRITABLE(ref_index_buf[i]);
  503. }
  504. return 0;
  505. }
  506. /**
  507. * Allocate a Picture.
  508. * The pixels are allocated/set by calling get_buffer() if shared = 0
  509. */
  510. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
  511. {
  512. int i, ret;
  513. if (pic->qscale_table_buf)
  514. if ( pic->alloc_mb_width != s->mb_width
  515. || pic->alloc_mb_height != s->mb_height)
  516. ff_free_picture_tables(pic);
  517. if (shared) {
  518. av_assert0(pic->f.data[0]);
  519. pic->shared = 1;
  520. } else {
  521. av_assert0(!pic->f.buf[0]);
  522. if (alloc_frame_buffer(s, pic) < 0)
  523. return -1;
  524. s->linesize = pic->f.linesize[0];
  525. s->uvlinesize = pic->f.linesize[1];
  526. }
  527. if (!pic->qscale_table_buf)
  528. ret = alloc_picture_tables(s, pic);
  529. else
  530. ret = make_tables_writable(pic);
  531. if (ret < 0)
  532. goto fail;
  533. if (s->encoding) {
  534. pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
  535. pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
  536. pic->mb_mean = pic->mb_mean_buf->data;
  537. }
  538. pic->mbskip_table = pic->mbskip_table_buf->data;
  539. pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
  540. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
  541. if (pic->motion_val_buf[0]) {
  542. for (i = 0; i < 2; i++) {
  543. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  544. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  545. }
  546. }
  547. return 0;
  548. fail:
  549. av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
  550. ff_mpeg_unref_picture(s, pic);
  551. ff_free_picture_tables(pic);
  552. return AVERROR(ENOMEM);
  553. }
  554. /**
  555. * Deallocate a picture.
  556. */
  557. void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
  558. {
  559. int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
  560. pic->tf.f = &pic->f;
  561. /* WM Image / Screen codecs allocate internal buffers with different
  562. * dimensions / colorspaces; ignore user-defined callbacks for these. */
  563. if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  564. s->codec_id != AV_CODEC_ID_VC1IMAGE &&
  565. s->codec_id != AV_CODEC_ID_MSS2)
  566. ff_thread_release_buffer(s->avctx, &pic->tf);
  567. else
  568. av_frame_unref(&pic->f);
  569. av_buffer_unref(&pic->hwaccel_priv_buf);
  570. if (pic->needs_realloc)
  571. ff_free_picture_tables(pic);
  572. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  573. }
  574. static int update_picture_tables(Picture *dst, Picture *src)
  575. {
  576. int i;
  577. #define UPDATE_TABLE(table)\
  578. do {\
  579. if (src->table &&\
  580. (!dst->table || dst->table->buffer != src->table->buffer)) {\
  581. av_buffer_unref(&dst->table);\
  582. dst->table = av_buffer_ref(src->table);\
  583. if (!dst->table) {\
  584. ff_free_picture_tables(dst);\
  585. return AVERROR(ENOMEM);\
  586. }\
  587. }\
  588. } while (0)
  589. UPDATE_TABLE(mb_var_buf);
  590. UPDATE_TABLE(mc_mb_var_buf);
  591. UPDATE_TABLE(mb_mean_buf);
  592. UPDATE_TABLE(mbskip_table_buf);
  593. UPDATE_TABLE(qscale_table_buf);
  594. UPDATE_TABLE(mb_type_buf);
  595. for (i = 0; i < 2; i++) {
  596. UPDATE_TABLE(motion_val_buf[i]);
  597. UPDATE_TABLE(ref_index_buf[i]);
  598. }
  599. dst->mb_var = src->mb_var;
  600. dst->mc_mb_var = src->mc_mb_var;
  601. dst->mb_mean = src->mb_mean;
  602. dst->mbskip_table = src->mbskip_table;
  603. dst->qscale_table = src->qscale_table;
  604. dst->mb_type = src->mb_type;
  605. for (i = 0; i < 2; i++) {
  606. dst->motion_val[i] = src->motion_val[i];
  607. dst->ref_index[i] = src->ref_index[i];
  608. }
  609. dst->alloc_mb_width = src->alloc_mb_width;
  610. dst->alloc_mb_height = src->alloc_mb_height;
  611. return 0;
  612. }
  613. int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
  614. {
  615. int ret;
  616. av_assert0(!dst->f.buf[0]);
  617. av_assert0(src->f.buf[0]);
  618. src->tf.f = &src->f;
  619. dst->tf.f = &dst->f;
  620. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  621. if (ret < 0)
  622. goto fail;
  623. ret = update_picture_tables(dst, src);
  624. if (ret < 0)
  625. goto fail;
  626. if (src->hwaccel_picture_private) {
  627. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  628. if (!dst->hwaccel_priv_buf)
  629. goto fail;
  630. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  631. }
  632. dst->field_picture = src->field_picture;
  633. dst->mb_var_sum = src->mb_var_sum;
  634. dst->mc_mb_var_sum = src->mc_mb_var_sum;
  635. dst->b_frame_score = src->b_frame_score;
  636. dst->needs_realloc = src->needs_realloc;
  637. dst->reference = src->reference;
  638. dst->shared = src->shared;
  639. return 0;
  640. fail:
  641. ff_mpeg_unref_picture(s, dst);
  642. return ret;
  643. }
  644. static void exchange_uv(MpegEncContext *s)
  645. {
  646. int16_t (*tmp)[64];
  647. tmp = s->pblocks[4];
  648. s->pblocks[4] = s->pblocks[5];
  649. s->pblocks[5] = tmp;
  650. }
  651. static int init_duplicate_context(MpegEncContext *s)
  652. {
  653. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  654. int c_size = s->mb_stride * (s->mb_height + 1);
  655. int yc_size = y_size + 2 * c_size;
  656. int i;
  657. if (s->mb_height & 1)
  658. yc_size += 2*s->b8_stride + 2*s->mb_stride;
  659. s->edge_emu_buffer =
  660. s->me.scratchpad =
  661. s->me.temp =
  662. s->rd_scratchpad =
  663. s->b_scratchpad =
  664. s->obmc_scratchpad = NULL;
  665. if (s->encoding) {
  666. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
  667. ME_MAP_SIZE * sizeof(uint32_t), fail)
  668. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
  669. ME_MAP_SIZE * sizeof(uint32_t), fail)
  670. if (s->avctx->noise_reduction) {
  671. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
  672. 2 * 64 * sizeof(int), fail)
  673. }
  674. }
  675. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
  676. s->block = s->blocks[0];
  677. for (i = 0; i < 12; i++) {
  678. s->pblocks[i] = &s->block[i];
  679. }
  680. if (s->avctx->codec_tag == AV_RL32("VCR2"))
  681. exchange_uv(s);
  682. if (s->out_format == FMT_H263) {
  683. /* ac values */
  684. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
  685. yc_size * sizeof(int16_t) * 16, fail);
  686. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  687. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  688. s->ac_val[2] = s->ac_val[1] + c_size;
  689. }
  690. return 0;
  691. fail:
  692. return -1; // free() through ff_MPV_common_end()
  693. }
  694. static void free_duplicate_context(MpegEncContext *s)
  695. {
  696. if (s == NULL)
  697. return;
  698. av_freep(&s->edge_emu_buffer);
  699. av_freep(&s->me.scratchpad);
  700. s->me.temp =
  701. s->rd_scratchpad =
  702. s->b_scratchpad =
  703. s->obmc_scratchpad = NULL;
  704. av_freep(&s->dct_error_sum);
  705. av_freep(&s->me.map);
  706. av_freep(&s->me.score_map);
  707. av_freep(&s->blocks);
  708. av_freep(&s->ac_val_base);
  709. s->block = NULL;
  710. }
  711. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
  712. {
  713. #define COPY(a) bak->a = src->a
  714. COPY(edge_emu_buffer);
  715. COPY(me.scratchpad);
  716. COPY(me.temp);
  717. COPY(rd_scratchpad);
  718. COPY(b_scratchpad);
  719. COPY(obmc_scratchpad);
  720. COPY(me.map);
  721. COPY(me.score_map);
  722. COPY(blocks);
  723. COPY(block);
  724. COPY(start_mb_y);
  725. COPY(end_mb_y);
  726. COPY(me.map_generation);
  727. COPY(pb);
  728. COPY(dct_error_sum);
  729. COPY(dct_count[0]);
  730. COPY(dct_count[1]);
  731. COPY(ac_val_base);
  732. COPY(ac_val[0]);
  733. COPY(ac_val[1]);
  734. COPY(ac_val[2]);
  735. #undef COPY
  736. }
  737. int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
  738. {
  739. MpegEncContext bak;
  740. int i, ret;
  741. // FIXME copy only needed parts
  742. // START_TIMER
  743. backup_duplicate_context(&bak, dst);
  744. memcpy(dst, src, sizeof(MpegEncContext));
  745. backup_duplicate_context(dst, &bak);
  746. for (i = 0; i < 12; i++) {
  747. dst->pblocks[i] = &dst->block[i];
  748. }
  749. if (dst->avctx->codec_tag == AV_RL32("VCR2"))
  750. exchange_uv(dst);
  751. if (!dst->edge_emu_buffer &&
  752. (ret = frame_size_alloc(dst, dst->linesize)) < 0) {
  753. av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
  754. "scratch buffers.\n");
  755. return ret;
  756. }
  757. // STOP_TIMER("update_duplicate_context")
  758. // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  759. return 0;
  760. }
  761. int ff_mpeg_update_thread_context(AVCodecContext *dst,
  762. const AVCodecContext *src)
  763. {
  764. int i, ret;
  765. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  766. if (dst == src)
  767. return 0;
  768. av_assert0(s != s1);
  769. // FIXME can parameters change on I-frames?
  770. // in that case dst may need a reinit
  771. if (!s->context_initialized) {
  772. memcpy(s, s1, sizeof(MpegEncContext));
  773. s->avctx = dst;
  774. s->bitstream_buffer = NULL;
  775. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  776. if (s1->context_initialized){
  777. // s->picture_range_start += MAX_PICTURE_COUNT;
  778. // s->picture_range_end += MAX_PICTURE_COUNT;
  779. if((ret = ff_MPV_common_init(s)) < 0){
  780. memset(s, 0, sizeof(MpegEncContext));
  781. s->avctx = dst;
  782. return ret;
  783. }
  784. }
  785. }
  786. if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
  787. s->context_reinit = 0;
  788. s->height = s1->height;
  789. s->width = s1->width;
  790. if ((ret = ff_MPV_common_frame_size_change(s)) < 0)
  791. return ret;
  792. }
  793. s->avctx->coded_height = s1->avctx->coded_height;
  794. s->avctx->coded_width = s1->avctx->coded_width;
  795. s->avctx->width = s1->avctx->width;
  796. s->avctx->height = s1->avctx->height;
  797. s->coded_picture_number = s1->coded_picture_number;
  798. s->picture_number = s1->picture_number;
  799. av_assert0(!s->picture || s->picture != s1->picture);
  800. if(s->picture)
  801. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  802. ff_mpeg_unref_picture(s, &s->picture[i]);
  803. if (s1->picture[i].f.buf[0] &&
  804. (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
  805. return ret;
  806. }
  807. #define UPDATE_PICTURE(pic)\
  808. do {\
  809. ff_mpeg_unref_picture(s, &s->pic);\
  810. if (s1->pic.f.buf[0])\
  811. ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
  812. else\
  813. ret = update_picture_tables(&s->pic, &s1->pic);\
  814. if (ret < 0)\
  815. return ret;\
  816. } while (0)
  817. UPDATE_PICTURE(current_picture);
  818. UPDATE_PICTURE(last_picture);
  819. UPDATE_PICTURE(next_picture);
  820. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  821. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  822. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  823. // Error/bug resilience
  824. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  825. s->workaround_bugs = s1->workaround_bugs;
  826. s->padding_bug_score = s1->padding_bug_score;
  827. // MPEG4 timing info
  828. memcpy(&s->last_time_base, &s1->last_time_base,
  829. (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
  830. (char *) &s1->last_time_base);
  831. // B-frame info
  832. s->max_b_frames = s1->max_b_frames;
  833. s->low_delay = s1->low_delay;
  834. s->droppable = s1->droppable;
  835. // DivX handling (doesn't work)
  836. s->divx_packed = s1->divx_packed;
  837. if (s1->bitstream_buffer) {
  838. if (s1->bitstream_buffer_size +
  839. FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  840. av_fast_malloc(&s->bitstream_buffer,
  841. &s->allocated_bitstream_buffer_size,
  842. s1->allocated_bitstream_buffer_size);
  843. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  844. memcpy(s->bitstream_buffer, s1->bitstream_buffer,
  845. s1->bitstream_buffer_size);
  846. memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
  847. FF_INPUT_BUFFER_PADDING_SIZE);
  848. }
  849. // linesize dependend scratch buffer allocation
  850. if (!s->edge_emu_buffer)
  851. if (s1->linesize) {
  852. if (frame_size_alloc(s, s1->linesize) < 0) {
  853. av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
  854. "scratch buffers.\n");
  855. return AVERROR(ENOMEM);
  856. }
  857. } else {
  858. av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
  859. "be allocated due to unknown size.\n");
  860. }
  861. // MPEG2/interlacing info
  862. memcpy(&s->progressive_sequence, &s1->progressive_sequence,
  863. (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
  864. if (!s1->first_field) {
  865. s->last_pict_type = s1->pict_type;
  866. if (s1->current_picture_ptr)
  867. s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
  868. }
  869. return 0;
  870. }
  871. /**
  872. * Set the given MpegEncContext to common defaults
  873. * (same for encoding and decoding).
  874. * The changed fields will not depend upon the
  875. * prior state of the MpegEncContext.
  876. */
  877. void ff_MPV_common_defaults(MpegEncContext *s)
  878. {
  879. s->y_dc_scale_table =
  880. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  881. s->chroma_qscale_table = ff_default_chroma_qscale_table;
  882. s->progressive_frame = 1;
  883. s->progressive_sequence = 1;
  884. s->picture_structure = PICT_FRAME;
  885. s->coded_picture_number = 0;
  886. s->picture_number = 0;
  887. s->f_code = 1;
  888. s->b_code = 1;
  889. s->slice_context_count = 1;
  890. }
  891. /**
  892. * Set the given MpegEncContext to defaults for decoding.
  893. * the changed fields will not depend upon
  894. * the prior state of the MpegEncContext.
  895. */
  896. void ff_MPV_decode_defaults(MpegEncContext *s)
  897. {
  898. ff_MPV_common_defaults(s);
  899. }
  900. static int init_er(MpegEncContext *s)
  901. {
  902. ERContext *er = &s->er;
  903. int mb_array_size = s->mb_height * s->mb_stride;
  904. int i;
  905. er->avctx = s->avctx;
  906. er->dsp = &s->dsp;
  907. er->mb_index2xy = s->mb_index2xy;
  908. er->mb_num = s->mb_num;
  909. er->mb_width = s->mb_width;
  910. er->mb_height = s->mb_height;
  911. er->mb_stride = s->mb_stride;
  912. er->b8_stride = s->b8_stride;
  913. er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride);
  914. er->error_status_table = av_mallocz(mb_array_size);
  915. if (!er->er_temp_buffer || !er->error_status_table)
  916. goto fail;
  917. er->mbskip_table = s->mbskip_table;
  918. er->mbintra_table = s->mbintra_table;
  919. for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
  920. er->dc_val[i] = s->dc_val[i];
  921. er->decode_mb = mpeg_er_decode_mb;
  922. er->opaque = s;
  923. return 0;
  924. fail:
  925. av_freep(&er->er_temp_buffer);
  926. av_freep(&er->error_status_table);
  927. return AVERROR(ENOMEM);
  928. }
  929. /**
  930. * Initialize and allocates MpegEncContext fields dependent on the resolution.
  931. */
  932. static int init_context_frame(MpegEncContext *s)
  933. {
  934. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
  935. s->mb_width = (s->width + 15) / 16;
  936. s->mb_stride = s->mb_width + 1;
  937. s->b8_stride = s->mb_width * 2 + 1;
  938. s->b4_stride = s->mb_width * 4 + 1;
  939. mb_array_size = s->mb_height * s->mb_stride;
  940. mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
  941. /* set default edge pos, will be overriden
  942. * in decode_header if needed */
  943. s->h_edge_pos = s->mb_width * 16;
  944. s->v_edge_pos = s->mb_height * 16;
  945. s->mb_num = s->mb_width * s->mb_height;
  946. s->block_wrap[0] =
  947. s->block_wrap[1] =
  948. s->block_wrap[2] =
  949. s->block_wrap[3] = s->b8_stride;
  950. s->block_wrap[4] =
  951. s->block_wrap[5] = s->mb_stride;
  952. y_size = s->b8_stride * (2 * s->mb_height + 1);
  953. c_size = s->mb_stride * (s->mb_height + 1);
  954. yc_size = y_size + 2 * c_size;
  955. if (s->mb_height & 1)
  956. yc_size += 2*s->b8_stride + 2*s->mb_stride;
  957. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); // error ressilience code looks cleaner with this
  958. for (y = 0; y < s->mb_height; y++)
  959. for (x = 0; x < s->mb_width; x++)
  960. s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
  961. s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
  962. if (s->encoding) {
  963. /* Allocate MV tables */
  964. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  965. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  966. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  967. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  968. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  969. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
  970. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  971. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  972. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  973. s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  974. s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  975. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  976. /* Allocate MB type table */
  977. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
  978. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
  979. FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
  980. mb_array_size * sizeof(float), fail);
  981. FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
  982. mb_array_size * sizeof(float), fail);
  983. }
  984. if (s->codec_id == AV_CODEC_ID_MPEG4 ||
  985. (s->flags & CODEC_FLAG_INTERLACED_ME)) {
  986. /* interlaced direct mode decoding tables */
  987. for (i = 0; i < 2; i++) {
  988. int j, k;
  989. for (j = 0; j < 2; j++) {
  990. for (k = 0; k < 2; k++) {
  991. FF_ALLOCZ_OR_GOTO(s->avctx,
  992. s->b_field_mv_table_base[i][j][k],
  993. mv_table_size * 2 * sizeof(int16_t),
  994. fail);
  995. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
  996. s->mb_stride + 1;
  997. }
  998. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
  999. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
  1000. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
  1001. }
  1002. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
  1003. }
  1004. }
  1005. if (s->out_format == FMT_H263) {
  1006. /* cbp values */
  1007. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size + (s->mb_height&1)*2*s->b8_stride, fail);
  1008. s->coded_block = s->coded_block_base + s->b8_stride + 1;
  1009. /* cbp, ac_pred, pred_dir */
  1010. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail);
  1011. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
  1012. }
  1013. if (s->h263_pred || s->h263_plus || !s->encoding) {
  1014. /* dc values */
  1015. // MN: we need these for error resilience of intra-frames
  1016. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
  1017. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  1018. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  1019. s->dc_val[2] = s->dc_val[1] + c_size;
  1020. for (i = 0; i < yc_size; i++)
  1021. s->dc_val_base[i] = 1024;
  1022. }
  1023. /* which mb is a intra block */
  1024. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  1025. memset(s->mbintra_table, 1, mb_array_size);
  1026. /* init macroblock skip table */
  1027. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
  1028. // Note the + 1 is for a quicker mpeg4 slice_end detection
  1029. return init_er(s);
  1030. fail:
  1031. return AVERROR(ENOMEM);
  1032. }
  1033. /**
  1034. * init common structure for both encoder and decoder.
  1035. * this assumes that some variables like width/height are already set
  1036. */
  1037. av_cold int ff_MPV_common_init(MpegEncContext *s)
  1038. {
  1039. int i;
  1040. int nb_slices = (HAVE_THREADS &&
  1041. s->avctx->active_thread_type & FF_THREAD_SLICE) ?
  1042. s->avctx->thread_count : 1;
  1043. if (s->encoding && s->avctx->slices)
  1044. nb_slices = s->avctx->slices;
  1045. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1046. s->mb_height = (s->height + 31) / 32 * 2;
  1047. else
  1048. s->mb_height = (s->height + 15) / 16;
  1049. if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  1050. av_log(s->avctx, AV_LOG_ERROR,
  1051. "decoding to AV_PIX_FMT_NONE is not supported.\n");
  1052. return -1;
  1053. }
  1054. if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
  1055. int max_slices;
  1056. if (s->mb_height)
  1057. max_slices = FFMIN(MAX_THREADS, s->mb_height);
  1058. else
  1059. max_slices = MAX_THREADS;
  1060. av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
  1061. " reducing to %d\n", nb_slices, max_slices);
  1062. nb_slices = max_slices;
  1063. }
  1064. if ((s->width || s->height) &&
  1065. av_image_check_size(s->width, s->height, 0, s->avctx))
  1066. return -1;
  1067. ff_dct_common_init(s);
  1068. s->flags = s->avctx->flags;
  1069. s->flags2 = s->avctx->flags2;
  1070. /* set chroma shifts */
  1071. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,
  1072. &s->chroma_x_shift,
  1073. &s->chroma_y_shift);
  1074. /* convert fourcc to upper case */
  1075. s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
  1076. s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
  1077. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
  1078. MAX_PICTURE_COUNT * sizeof(Picture), fail);
  1079. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1080. av_frame_unref(&s->picture[i].f);
  1081. }
  1082. memset(&s->next_picture, 0, sizeof(s->next_picture));
  1083. memset(&s->last_picture, 0, sizeof(s->last_picture));
  1084. memset(&s->current_picture, 0, sizeof(s->current_picture));
  1085. av_frame_unref(&s->next_picture.f);
  1086. av_frame_unref(&s->last_picture.f);
  1087. av_frame_unref(&s->current_picture.f);
  1088. if (init_context_frame(s))
  1089. goto fail;
  1090. s->parse_context.state = -1;
  1091. s->context_initialized = 1;
  1092. s->thread_context[0] = s;
  1093. // if (s->width && s->height) {
  1094. if (nb_slices > 1) {
  1095. for (i = 1; i < nb_slices; i++) {
  1096. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1097. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1098. }
  1099. for (i = 0; i < nb_slices; i++) {
  1100. if (init_duplicate_context(s->thread_context[i]) < 0)
  1101. goto fail;
  1102. s->thread_context[i]->start_mb_y =
  1103. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1104. s->thread_context[i]->end_mb_y =
  1105. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1106. }
  1107. } else {
  1108. if (init_duplicate_context(s) < 0)
  1109. goto fail;
  1110. s->start_mb_y = 0;
  1111. s->end_mb_y = s->mb_height;
  1112. }
  1113. s->slice_context_count = nb_slices;
  1114. // }
  1115. return 0;
  1116. fail:
  1117. ff_MPV_common_end(s);
  1118. return -1;
  1119. }
  1120. /**
  1121. * Frees and resets MpegEncContext fields depending on the resolution.
  1122. * Is used during resolution changes to avoid a full reinitialization of the
  1123. * codec.
  1124. */
  1125. static int free_context_frame(MpegEncContext *s)
  1126. {
  1127. int i, j, k;
  1128. av_freep(&s->mb_type);
  1129. av_freep(&s->p_mv_table_base);
  1130. av_freep(&s->b_forw_mv_table_base);
  1131. av_freep(&s->b_back_mv_table_base);
  1132. av_freep(&s->b_bidir_forw_mv_table_base);
  1133. av_freep(&s->b_bidir_back_mv_table_base);
  1134. av_freep(&s->b_direct_mv_table_base);
  1135. s->p_mv_table = NULL;
  1136. s->b_forw_mv_table = NULL;
  1137. s->b_back_mv_table = NULL;
  1138. s->b_bidir_forw_mv_table = NULL;
  1139. s->b_bidir_back_mv_table = NULL;
  1140. s->b_direct_mv_table = NULL;
  1141. for (i = 0; i < 2; i++) {
  1142. for (j = 0; j < 2; j++) {
  1143. for (k = 0; k < 2; k++) {
  1144. av_freep(&s->b_field_mv_table_base[i][j][k]);
  1145. s->b_field_mv_table[i][j][k] = NULL;
  1146. }
  1147. av_freep(&s->b_field_select_table[i][j]);
  1148. av_freep(&s->p_field_mv_table_base[i][j]);
  1149. s->p_field_mv_table[i][j] = NULL;
  1150. }
  1151. av_freep(&s->p_field_select_table[i]);
  1152. }
  1153. av_freep(&s->dc_val_base);
  1154. av_freep(&s->coded_block_base);
  1155. av_freep(&s->mbintra_table);
  1156. av_freep(&s->cbp_table);
  1157. av_freep(&s->pred_dir_table);
  1158. av_freep(&s->mbskip_table);
  1159. av_freep(&s->er.error_status_table);
  1160. av_freep(&s->er.er_temp_buffer);
  1161. av_freep(&s->mb_index2xy);
  1162. av_freep(&s->lambda_table);
  1163. av_freep(&s->cplx_tab);
  1164. av_freep(&s->bits_tab);
  1165. s->linesize = s->uvlinesize = 0;
  1166. return 0;
  1167. }
  1168. int ff_MPV_common_frame_size_change(MpegEncContext *s)
  1169. {
  1170. int i, err = 0;
  1171. if (s->slice_context_count > 1) {
  1172. for (i = 0; i < s->slice_context_count; i++) {
  1173. free_duplicate_context(s->thread_context[i]);
  1174. }
  1175. for (i = 1; i < s->slice_context_count; i++) {
  1176. av_freep(&s->thread_context[i]);
  1177. }
  1178. } else
  1179. free_duplicate_context(s);
  1180. if ((err = free_context_frame(s)) < 0)
  1181. return err;
  1182. if (s->picture)
  1183. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1184. s->picture[i].needs_realloc = 1;
  1185. }
  1186. s->last_picture_ptr =
  1187. s->next_picture_ptr =
  1188. s->current_picture_ptr = NULL;
  1189. // init
  1190. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  1191. s->mb_height = (s->height + 31) / 32 * 2;
  1192. else
  1193. s->mb_height = (s->height + 15) / 16;
  1194. if ((s->width || s->height) &&
  1195. av_image_check_size(s->width, s->height, 0, s->avctx))
  1196. return AVERROR_INVALIDDATA;
  1197. if ((err = init_context_frame(s)))
  1198. goto fail;
  1199. s->thread_context[0] = s;
  1200. if (s->width && s->height) {
  1201. int nb_slices = s->slice_context_count;
  1202. if (nb_slices > 1) {
  1203. for (i = 1; i < nb_slices; i++) {
  1204. s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
  1205. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  1206. }
  1207. for (i = 0; i < nb_slices; i++) {
  1208. if (init_duplicate_context(s->thread_context[i]) < 0)
  1209. goto fail;
  1210. s->thread_context[i]->start_mb_y =
  1211. (s->mb_height * (i) + nb_slices / 2) / nb_slices;
  1212. s->thread_context[i]->end_mb_y =
  1213. (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
  1214. }
  1215. } else {
  1216. err = init_duplicate_context(s);
  1217. if (err < 0)
  1218. goto fail;
  1219. s->start_mb_y = 0;
  1220. s->end_mb_y = s->mb_height;
  1221. }
  1222. s->slice_context_count = nb_slices;
  1223. }
  1224. return 0;
  1225. fail:
  1226. ff_MPV_common_end(s);
  1227. return err;
  1228. }
  1229. /* init common structure for both encoder and decoder */
  1230. void ff_MPV_common_end(MpegEncContext *s)
  1231. {
  1232. int i;
  1233. if (s->slice_context_count > 1) {
  1234. for (i = 0; i < s->slice_context_count; i++) {
  1235. free_duplicate_context(s->thread_context[i]);
  1236. }
  1237. for (i = 1; i < s->slice_context_count; i++) {
  1238. av_freep(&s->thread_context[i]);
  1239. }
  1240. s->slice_context_count = 1;
  1241. } else free_duplicate_context(s);
  1242. av_freep(&s->parse_context.buffer);
  1243. s->parse_context.buffer_size = 0;
  1244. av_freep(&s->bitstream_buffer);
  1245. s->allocated_bitstream_buffer_size = 0;
  1246. if (s->picture) {
  1247. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1248. ff_free_picture_tables(&s->picture[i]);
  1249. ff_mpeg_unref_picture(s, &s->picture[i]);
  1250. }
  1251. }
  1252. av_freep(&s->picture);
  1253. ff_free_picture_tables(&s->last_picture);
  1254. ff_mpeg_unref_picture(s, &s->last_picture);
  1255. ff_free_picture_tables(&s->current_picture);
  1256. ff_mpeg_unref_picture(s, &s->current_picture);
  1257. ff_free_picture_tables(&s->next_picture);
  1258. ff_mpeg_unref_picture(s, &s->next_picture);
  1259. free_context_frame(s);
  1260. s->context_initialized = 0;
  1261. s->last_picture_ptr =
  1262. s->next_picture_ptr =
  1263. s->current_picture_ptr = NULL;
  1264. s->linesize = s->uvlinesize = 0;
  1265. }
  1266. av_cold void ff_init_rl(RLTable *rl,
  1267. uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
  1268. {
  1269. int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
  1270. uint8_t index_run[MAX_RUN + 1];
  1271. int last, run, level, start, end, i;
  1272. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  1273. if (static_store && rl->max_level[0])
  1274. return;
  1275. /* compute max_level[], max_run[] and index_run[] */
  1276. for (last = 0; last < 2; last++) {
  1277. if (last == 0) {
  1278. start = 0;
  1279. end = rl->last;
  1280. } else {
  1281. start = rl->last;
  1282. end = rl->n;
  1283. }
  1284. memset(max_level, 0, MAX_RUN + 1);
  1285. memset(max_run, 0, MAX_LEVEL + 1);
  1286. memset(index_run, rl->n, MAX_RUN + 1);
  1287. for (i = start; i < end; i++) {
  1288. run = rl->table_run[i];
  1289. level = rl->table_level[i];
  1290. if (index_run[run] == rl->n)
  1291. index_run[run] = i;
  1292. if (level > max_level[run])
  1293. max_level[run] = level;
  1294. if (run > max_run[level])
  1295. max_run[level] = run;
  1296. }
  1297. if (static_store)
  1298. rl->max_level[last] = static_store[last];
  1299. else
  1300. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  1301. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  1302. if (static_store)
  1303. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  1304. else
  1305. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  1306. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  1307. if (static_store)
  1308. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  1309. else
  1310. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  1311. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  1312. }
  1313. }
  1314. av_cold void ff_init_vlc_rl(RLTable *rl)
  1315. {
  1316. int i, q;
  1317. for (q = 0; q < 32; q++) {
  1318. int qmul = q * 2;
  1319. int qadd = (q - 1) | 1;
  1320. if (q == 0) {
  1321. qmul = 1;
  1322. qadd = 0;
  1323. }
  1324. for (i = 0; i < rl->vlc.table_size; i++) {
  1325. int code = rl->vlc.table[i][0];
  1326. int len = rl->vlc.table[i][1];
  1327. int level, run;
  1328. if (len == 0) { // illegal code
  1329. run = 66;
  1330. level = MAX_LEVEL;
  1331. } else if (len < 0) { // more bits needed
  1332. run = 0;
  1333. level = code;
  1334. } else {
  1335. if (code == rl->n) { // esc
  1336. run = 66;
  1337. level = 0;
  1338. } else {
  1339. run = rl->table_run[code] + 1;
  1340. level = rl->table_level[code] * qmul + qadd;
  1341. if (code >= rl->last) run += 192;
  1342. }
  1343. }
  1344. rl->rl_vlc[q][i].len = len;
  1345. rl->rl_vlc[q][i].level = level;
  1346. rl->rl_vlc[q][i].run = run;
  1347. }
  1348. }
  1349. }
  1350. static void release_unused_pictures(MpegEncContext *s)
  1351. {
  1352. int i;
  1353. /* release non reference frames */
  1354. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1355. if (!s->picture[i].reference)
  1356. ff_mpeg_unref_picture(s, &s->picture[i]);
  1357. }
  1358. }
  1359. static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
  1360. {
  1361. if (pic == s->last_picture_ptr)
  1362. return 0;
  1363. if (pic->f.buf[0] == NULL)
  1364. return 1;
  1365. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  1366. return 1;
  1367. return 0;
  1368. }
  1369. static int find_unused_picture(MpegEncContext *s, int shared)
  1370. {
  1371. int i;
  1372. if (shared) {
  1373. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1374. if (s->picture[i].f.buf[0] == NULL && &s->picture[i] != s->last_picture_ptr)
  1375. return i;
  1376. }
  1377. } else {
  1378. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1379. if (pic_is_unused(s, &s->picture[i]))
  1380. return i;
  1381. }
  1382. }
  1383. av_log(s->avctx, AV_LOG_FATAL,
  1384. "Internal error, picture buffer overflow\n");
  1385. /* We could return -1, but the codec would crash trying to draw into a
  1386. * non-existing frame anyway. This is safer than waiting for a random crash.
  1387. * Also the return of this is never useful, an encoder must only allocate
  1388. * as much as allowed in the specification. This has no relationship to how
  1389. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  1390. * enough for such valid streams).
  1391. * Plus, a decoder has to check stream validity and remove frames if too
  1392. * many reference frames are around. Waiting for "OOM" is not correct at
  1393. * all. Similarly, missing reference frames have to be replaced by
  1394. * interpolated/MC frames, anything else is a bug in the codec ...
  1395. */
  1396. abort();
  1397. return -1;
  1398. }
  1399. int ff_find_unused_picture(MpegEncContext *s, int shared)
  1400. {
  1401. int ret = find_unused_picture(s, shared);
  1402. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  1403. if (s->picture[ret].needs_realloc) {
  1404. s->picture[ret].needs_realloc = 0;
  1405. ff_free_picture_tables(&s->picture[ret]);
  1406. ff_mpeg_unref_picture(s, &s->picture[ret]);
  1407. }
  1408. }
  1409. return ret;
  1410. }
  1411. /**
  1412. * generic function called after decoding
  1413. * the header and before a frame is decoded.
  1414. */
  1415. int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  1416. {
  1417. int i, ret;
  1418. Picture *pic;
  1419. s->mb_skipped = 0;
  1420. if (!ff_thread_can_start_frame(avctx)) {
  1421. av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
  1422. return -1;
  1423. }
  1424. /* mark & release old frames */
  1425. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
  1426. s->last_picture_ptr != s->next_picture_ptr &&
  1427. s->last_picture_ptr->f.buf[0]) {
  1428. ff_mpeg_unref_picture(s, s->last_picture_ptr);
  1429. }
  1430. /* release forgotten pictures */
  1431. /* if (mpeg124/h263) */
  1432. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  1433. if (&s->picture[i] != s->last_picture_ptr &&
  1434. &s->picture[i] != s->next_picture_ptr &&
  1435. s->picture[i].reference && !s->picture[i].needs_realloc) {
  1436. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  1437. av_log(avctx, AV_LOG_ERROR,
  1438. "releasing zombie picture\n");
  1439. ff_mpeg_unref_picture(s, &s->picture[i]);
  1440. }
  1441. }
  1442. ff_mpeg_unref_picture(s, &s->current_picture);
  1443. release_unused_pictures(s);
  1444. if (s->current_picture_ptr &&
  1445. s->current_picture_ptr->f.buf[0] == NULL) {
  1446. // we already have a unused image
  1447. // (maybe it was set before reading the header)
  1448. pic = s->current_picture_ptr;
  1449. } else {
  1450. i = ff_find_unused_picture(s, 0);
  1451. if (i < 0) {
  1452. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1453. return i;
  1454. }
  1455. pic = &s->picture[i];
  1456. }
  1457. pic->reference = 0;
  1458. if (!s->droppable) {
  1459. if (s->pict_type != AV_PICTURE_TYPE_B)
  1460. pic->reference = 3;
  1461. }
  1462. pic->f.coded_picture_number = s->coded_picture_number++;
  1463. if (ff_alloc_picture(s, pic, 0) < 0)
  1464. return -1;
  1465. s->current_picture_ptr = pic;
  1466. // FIXME use only the vars from current_pic
  1467. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  1468. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
  1469. s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1470. if (s->picture_structure != PICT_FRAME)
  1471. s->current_picture_ptr->f.top_field_first =
  1472. (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  1473. }
  1474. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame &&
  1475. !s->progressive_sequence;
  1476. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  1477. s->current_picture_ptr->f.pict_type = s->pict_type;
  1478. // if (s->flags && CODEC_FLAG_QSCALE)
  1479. // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
  1480. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1481. if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
  1482. s->current_picture_ptr)) < 0)
  1483. return ret;
  1484. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1485. s->last_picture_ptr = s->next_picture_ptr;
  1486. if (!s->droppable)
  1487. s->next_picture_ptr = s->current_picture_ptr;
  1488. }
  1489. av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
  1490. s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  1491. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  1492. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  1493. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  1494. s->pict_type, s->droppable);
  1495. if ((s->last_picture_ptr == NULL ||
  1496. s->last_picture_ptr->f.buf[0] == NULL) &&
  1497. (s->pict_type != AV_PICTURE_TYPE_I ||
  1498. s->picture_structure != PICT_FRAME)) {
  1499. int h_chroma_shift, v_chroma_shift;
  1500. av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
  1501. &h_chroma_shift, &v_chroma_shift);
  1502. if (s->pict_type == AV_PICTURE_TYPE_B && s->next_picture_ptr && s->next_picture_ptr->f.buf[0])
  1503. av_log(avctx, AV_LOG_DEBUG,
  1504. "allocating dummy last picture for B frame\n");
  1505. else if (s->pict_type != AV_PICTURE_TYPE_I)
  1506. av_log(avctx, AV_LOG_ERROR,
  1507. "warning: first frame is no keyframe\n");
  1508. else if (s->picture_structure != PICT_FRAME)
  1509. av_log(avctx, AV_LOG_DEBUG,
  1510. "allocate dummy last picture for field based first keyframe\n");
  1511. /* Allocate a dummy frame */
  1512. i = ff_find_unused_picture(s, 0);
  1513. if (i < 0) {
  1514. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1515. return i;
  1516. }
  1517. s->last_picture_ptr = &s->picture[i];
  1518. s->last_picture_ptr->reference = 3;
  1519. s->last_picture_ptr->f.key_frame = 0;
  1520. s->last_picture_ptr->f.pict_type = AV_PICTURE_TYPE_P;
  1521. if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
  1522. s->last_picture_ptr = NULL;
  1523. return -1;
  1524. }
  1525. if (!avctx->hwaccel) {
  1526. for(i=0; i<avctx->height; i++)
  1527. memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i,
  1528. 0x80, avctx->width);
  1529. for(i=0; i<FF_CEIL_RSHIFT(avctx->height, v_chroma_shift); i++) {
  1530. memset(s->last_picture_ptr->f.data[1] + s->last_picture_ptr->f.linesize[1]*i,
  1531. 0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
  1532. memset(s->last_picture_ptr->f.data[2] + s->last_picture_ptr->f.linesize[2]*i,
  1533. 0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
  1534. }
  1535. if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
  1536. for(i=0; i<avctx->height; i++)
  1537. memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i, 16, avctx->width);
  1538. }
  1539. }
  1540. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
  1541. ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
  1542. }
  1543. if ((s->next_picture_ptr == NULL ||
  1544. s->next_picture_ptr->f.buf[0] == NULL) &&
  1545. s->pict_type == AV_PICTURE_TYPE_B) {
  1546. /* Allocate a dummy frame */
  1547. i = ff_find_unused_picture(s, 0);
  1548. if (i < 0) {
  1549. av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
  1550. return i;
  1551. }
  1552. s->next_picture_ptr = &s->picture[i];
  1553. s->next_picture_ptr->reference = 3;
  1554. s->next_picture_ptr->f.key_frame = 0;
  1555. s->next_picture_ptr->f.pict_type = AV_PICTURE_TYPE_P;
  1556. if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
  1557. s->next_picture_ptr = NULL;
  1558. return -1;
  1559. }
  1560. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
  1561. ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
  1562. }
  1563. #if 0 // BUFREF-FIXME
  1564. memset(s->last_picture.f.data, 0, sizeof(s->last_picture.f.data));
  1565. memset(s->next_picture.f.data, 0, sizeof(s->next_picture.f.data));
  1566. #endif
  1567. if (s->last_picture_ptr) {
  1568. ff_mpeg_unref_picture(s, &s->last_picture);
  1569. if (s->last_picture_ptr->f.buf[0] &&
  1570. (ret = ff_mpeg_ref_picture(s, &s->last_picture,
  1571. s->last_picture_ptr)) < 0)
  1572. return ret;
  1573. }
  1574. if (s->next_picture_ptr) {
  1575. ff_mpeg_unref_picture(s, &s->next_picture);
  1576. if (s->next_picture_ptr->f.buf[0] &&
  1577. (ret = ff_mpeg_ref_picture(s, &s->next_picture,
  1578. s->next_picture_ptr)) < 0)
  1579. return ret;
  1580. }
  1581. av_assert0(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
  1582. s->last_picture_ptr->f.buf[0]));
  1583. if (s->picture_structure!= PICT_FRAME) {
  1584. int i;
  1585. for (i = 0; i < 4; i++) {
  1586. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1587. s->current_picture.f.data[i] +=
  1588. s->current_picture.f.linesize[i];
  1589. }
  1590. s->current_picture.f.linesize[i] *= 2;
  1591. s->last_picture.f.linesize[i] *= 2;
  1592. s->next_picture.f.linesize[i] *= 2;
  1593. }
  1594. }
  1595. s->err_recognition = avctx->err_recognition;
  1596. /* set dequantizer, we can't do it during init as
  1597. * it might change for mpeg4 and we can't do it in the header
  1598. * decode as init is not called for mpeg4 there yet */
  1599. if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  1600. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1601. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1602. } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
  1603. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1604. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1605. } else {
  1606. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1607. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1608. }
  1609. return 0;
  1610. }
  1611. /* called after a frame has been decoded. */
  1612. void ff_MPV_frame_end(MpegEncContext *s)
  1613. {
  1614. emms_c();
  1615. if (s->current_picture.reference)
  1616. ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
  1617. }
  1618. /**
  1619. * Draw a line from (ex, ey) -> (sx, sy).
  1620. * @param w width of the image
  1621. * @param h height of the image
  1622. * @param stride stride/linesize of the image
  1623. * @param color color of the arrow
  1624. */
  1625. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  1626. int w, int h, int stride, int color)
  1627. {
  1628. int x, y, fr, f;
  1629. sx = av_clip(sx, 0, w - 1);
  1630. sy = av_clip(sy, 0, h - 1);
  1631. ex = av_clip(ex, 0, w - 1);
  1632. ey = av_clip(ey, 0, h - 1);
  1633. buf[sy * stride + sx] += color;
  1634. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  1635. if (sx > ex) {
  1636. FFSWAP(int, sx, ex);
  1637. FFSWAP(int, sy, ey);
  1638. }
  1639. buf += sx + sy * stride;
  1640. ex -= sx;
  1641. f = ((ey - sy) << 16) / ex;
  1642. for (x = 0; x <= ex; x++) {
  1643. y = (x * f) >> 16;
  1644. fr = (x * f) & 0xFFFF;
  1645. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1646. if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  1647. }
  1648. } else {
  1649. if (sy > ey) {
  1650. FFSWAP(int, sx, ex);
  1651. FFSWAP(int, sy, ey);
  1652. }
  1653. buf += sx + sy * stride;
  1654. ey -= sy;
  1655. if (ey)
  1656. f = ((ex - sx) << 16) / ey;
  1657. else
  1658. f = 0;
  1659. for(y= 0; y <= ey; y++){
  1660. x = (y*f) >> 16;
  1661. fr = (y*f) & 0xFFFF;
  1662. buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
  1663. if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
  1664. }
  1665. }
  1666. }
  1667. /**
  1668. * Draw an arrow from (ex, ey) -> (sx, sy).
  1669. * @param w width of the image
  1670. * @param h height of the image
  1671. * @param stride stride/linesize of the image
  1672. * @param color color of the arrow
  1673. */
  1674. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  1675. int ey, int w, int h, int stride, int color)
  1676. {
  1677. int dx,dy;
  1678. sx = av_clip(sx, -100, w + 100);
  1679. sy = av_clip(sy, -100, h + 100);
  1680. ex = av_clip(ex, -100, w + 100);
  1681. ey = av_clip(ey, -100, h + 100);
  1682. dx = ex - sx;
  1683. dy = ey - sy;
  1684. if (dx * dx + dy * dy > 3 * 3) {
  1685. int rx = dx + dy;
  1686. int ry = -dx + dy;
  1687. int length = ff_sqrt((rx * rx + ry * ry) << 8);
  1688. // FIXME subpixel accuracy
  1689. rx = ROUNDED_DIV(rx * 3 << 4, length);
  1690. ry = ROUNDED_DIV(ry * 3 << 4, length);
  1691. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1692. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1693. }
  1694. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1695. }
  1696. /**
  1697. * Print debugging info for the given picture.
  1698. */
  1699. void ff_print_debug_info2(AVCodecContext *avctx, Picture *p, AVFrame *pict, uint8_t *mbskip_table,
  1700. int *low_delay,
  1701. int mb_width, int mb_height, int mb_stride, int quarter_sample)
  1702. {
  1703. if (avctx->hwaccel || !p || !p->mb_type
  1704. || (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
  1705. return;
  1706. if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  1707. int x,y;
  1708. av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
  1709. av_get_picture_type_char(pict->pict_type));
  1710. for (y = 0; y < mb_height; y++) {
  1711. for (x = 0; x < mb_width; x++) {
  1712. if (avctx->debug & FF_DEBUG_SKIP) {
  1713. int count = mbskip_table[x + y * mb_stride];
  1714. if (count > 9)
  1715. count = 9;
  1716. av_log(avctx, AV_LOG_DEBUG, "%1d", count);
  1717. }
  1718. if (avctx->debug & FF_DEBUG_QP) {
  1719. av_log(avctx, AV_LOG_DEBUG, "%2d",
  1720. p->qscale_table[x + y * mb_stride]);
  1721. }
  1722. if (avctx->debug & FF_DEBUG_MB_TYPE) {
  1723. int mb_type = p->mb_type[x + y * mb_stride];
  1724. // Type & MV direction
  1725. if (IS_PCM(mb_type))
  1726. av_log(avctx, AV_LOG_DEBUG, "P");
  1727. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1728. av_log(avctx, AV_LOG_DEBUG, "A");
  1729. else if (IS_INTRA4x4(mb_type))
  1730. av_log(avctx, AV_LOG_DEBUG, "i");
  1731. else if (IS_INTRA16x16(mb_type))
  1732. av_log(avctx, AV_LOG_DEBUG, "I");
  1733. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1734. av_log(avctx, AV_LOG_DEBUG, "d");
  1735. else if (IS_DIRECT(mb_type))
  1736. av_log(avctx, AV_LOG_DEBUG, "D");
  1737. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  1738. av_log(avctx, AV_LOG_DEBUG, "g");
  1739. else if (IS_GMC(mb_type))
  1740. av_log(avctx, AV_LOG_DEBUG, "G");
  1741. else if (IS_SKIP(mb_type))
  1742. av_log(avctx, AV_LOG_DEBUG, "S");
  1743. else if (!USES_LIST(mb_type, 1))
  1744. av_log(avctx, AV_LOG_DEBUG, ">");
  1745. else if (!USES_LIST(mb_type, 0))
  1746. av_log(avctx, AV_LOG_DEBUG, "<");
  1747. else {
  1748. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1749. av_log(avctx, AV_LOG_DEBUG, "X");
  1750. }
  1751. // segmentation
  1752. if (IS_8X8(mb_type))
  1753. av_log(avctx, AV_LOG_DEBUG, "+");
  1754. else if (IS_16X8(mb_type))
  1755. av_log(avctx, AV_LOG_DEBUG, "-");
  1756. else if (IS_8X16(mb_type))
  1757. av_log(avctx, AV_LOG_DEBUG, "|");
  1758. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  1759. av_log(avctx, AV_LOG_DEBUG, " ");
  1760. else
  1761. av_log(avctx, AV_LOG_DEBUG, "?");
  1762. if (IS_INTERLACED(mb_type))
  1763. av_log(avctx, AV_LOG_DEBUG, "=");
  1764. else
  1765. av_log(avctx, AV_LOG_DEBUG, " ");
  1766. }
  1767. }
  1768. av_log(avctx, AV_LOG_DEBUG, "\n");
  1769. }
  1770. }
  1771. if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  1772. (avctx->debug_mv)) {
  1773. const int shift = 1 + quarter_sample;
  1774. int mb_y;
  1775. uint8_t *ptr;
  1776. int i;
  1777. int h_chroma_shift, v_chroma_shift, block_height;
  1778. const int width = avctx->width;
  1779. const int height = avctx->height;
  1780. const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
  1781. const int mv_stride = (mb_width << mv_sample_log2) +
  1782. (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
  1783. *low_delay = 0; // needed to see the vectors without trashing the buffers
  1784. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1785. av_frame_make_writable(pict);
  1786. pict->opaque = NULL;
  1787. ptr = pict->data[0];
  1788. block_height = 16 >> v_chroma_shift;
  1789. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  1790. int mb_x;
  1791. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  1792. const int mb_index = mb_x + mb_y * mb_stride;
  1793. if ((avctx->debug_mv) && p->motion_val[0]) {
  1794. int type;
  1795. for (type = 0; type < 3; type++) {
  1796. int direction = 0;
  1797. switch (type) {
  1798. case 0:
  1799. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
  1800. (pict->pict_type!= AV_PICTURE_TYPE_P))
  1801. continue;
  1802. direction = 0;
  1803. break;
  1804. case 1:
  1805. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
  1806. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1807. continue;
  1808. direction = 0;
  1809. break;
  1810. case 2:
  1811. if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
  1812. (pict->pict_type!= AV_PICTURE_TYPE_B))
  1813. continue;
  1814. direction = 1;
  1815. break;
  1816. }
  1817. if (!USES_LIST(p->mb_type[mb_index], direction))
  1818. continue;
  1819. if (IS_8X8(p->mb_type[mb_index])) {
  1820. int i;
  1821. for (i = 0; i < 4; i++) {
  1822. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  1823. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  1824. int xy = (mb_x * 2 + (i & 1) +
  1825. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1826. int mx = (p->motion_val[direction][xy][0] >> shift) + sx;
  1827. int my = (p->motion_val[direction][xy][1] >> shift) + sy;
  1828. draw_arrow(ptr, sx, sy, mx, my, width,
  1829. height, pict->linesize[0], 100);
  1830. }
  1831. } else if (IS_16X8(p->mb_type[mb_index])) {
  1832. int i;
  1833. for (i = 0; i < 2; i++) {
  1834. int sx = mb_x * 16 + 8;
  1835. int sy = mb_y * 16 + 4 + 8 * i;
  1836. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  1837. int mx = (p->motion_val[direction][xy][0] >> shift);
  1838. int my = (p->motion_val[direction][xy][1] >> shift);
  1839. if (IS_INTERLACED(p->mb_type[mb_index]))
  1840. my *= 2;
  1841. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1842. height, pict->linesize[0], 100);
  1843. }
  1844. } else if (IS_8X16(p->mb_type[mb_index])) {
  1845. int i;
  1846. for (i = 0; i < 2; i++) {
  1847. int sx = mb_x * 16 + 4 + 8 * i;
  1848. int sy = mb_y * 16 + 8;
  1849. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  1850. int mx = p->motion_val[direction][xy][0] >> shift;
  1851. int my = p->motion_val[direction][xy][1] >> shift;
  1852. if (IS_INTERLACED(p->mb_type[mb_index]))
  1853. my *= 2;
  1854. draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
  1855. height, pict->linesize[0], 100);
  1856. }
  1857. } else {
  1858. int sx= mb_x * 16 + 8;
  1859. int sy= mb_y * 16 + 8;
  1860. int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
  1861. int mx= (p->motion_val[direction][xy][0]>>shift) + sx;
  1862. int my= (p->motion_val[direction][xy][1]>>shift) + sy;
  1863. draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100);
  1864. }
  1865. }
  1866. }
  1867. if ((avctx->debug & FF_DEBUG_VIS_QP)) {
  1868. uint64_t c = (p->qscale_table[mb_index] * 128 / 31) *
  1869. 0x0101010101010101ULL;
  1870. int y;
  1871. for (y = 0; y < block_height; y++) {
  1872. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1873. (block_height * mb_y + y) *
  1874. pict->linesize[1]) = c;
  1875. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1876. (block_height * mb_y + y) *
  1877. pict->linesize[2]) = c;
  1878. }
  1879. }
  1880. if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
  1881. p->motion_val[0]) {
  1882. int mb_type = p->mb_type[mb_index];
  1883. uint64_t u,v;
  1884. int y;
  1885. #define COLOR(theta, r) \
  1886. u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
  1887. v = (int)(128 + r * sin(theta * 3.141592 / 180));
  1888. u = v = 128;
  1889. if (IS_PCM(mb_type)) {
  1890. COLOR(120, 48)
  1891. } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
  1892. IS_INTRA16x16(mb_type)) {
  1893. COLOR(30, 48)
  1894. } else if (IS_INTRA4x4(mb_type)) {
  1895. COLOR(90, 48)
  1896. } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
  1897. // COLOR(120, 48)
  1898. } else if (IS_DIRECT(mb_type)) {
  1899. COLOR(150, 48)
  1900. } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
  1901. COLOR(170, 48)
  1902. } else if (IS_GMC(mb_type)) {
  1903. COLOR(190, 48)
  1904. } else if (IS_SKIP(mb_type)) {
  1905. // COLOR(180, 48)
  1906. } else if (!USES_LIST(mb_type, 1)) {
  1907. COLOR(240, 48)
  1908. } else if (!USES_LIST(mb_type, 0)) {
  1909. COLOR(0, 48)
  1910. } else {
  1911. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1912. COLOR(300,48)
  1913. }
  1914. u *= 0x0101010101010101ULL;
  1915. v *= 0x0101010101010101ULL;
  1916. for (y = 0; y < block_height; y++) {
  1917. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  1918. (block_height * mb_y + y) * pict->linesize[1]) = u;
  1919. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  1920. (block_height * mb_y + y) * pict->linesize[2]) = v;
  1921. }
  1922. // segmentation
  1923. if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
  1924. *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
  1925. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1926. *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
  1927. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  1928. }
  1929. if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
  1930. for (y = 0; y < 16; y++)
  1931. pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
  1932. pict->linesize[0]] ^= 0x80;
  1933. }
  1934. if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
  1935. int dm = 1 << (mv_sample_log2 - 2);
  1936. for (i = 0; i < 4; i++) {
  1937. int sx = mb_x * 16 + 8 * (i & 1);
  1938. int sy = mb_y * 16 + 8 * (i >> 1);
  1939. int xy = (mb_x * 2 + (i & 1) +
  1940. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  1941. // FIXME bidir
  1942. int32_t *mv = (int32_t *) &p->motion_val[0][xy];
  1943. if (mv[0] != mv[dm] ||
  1944. mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
  1945. for (y = 0; y < 8; y++)
  1946. pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
  1947. if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
  1948. *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
  1949. pict->linesize[0]) ^= 0x8080808080808080ULL;
  1950. }
  1951. }
  1952. if (IS_INTERLACED(mb_type) &&
  1953. avctx->codec->id == AV_CODEC_ID_H264) {
  1954. // hmm
  1955. }
  1956. }
  1957. mbskip_table[mb_index] = 0;
  1958. }
  1959. }
  1960. }
  1961. }
  1962. void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
  1963. {
  1964. ff_print_debug_info2(s->avctx, p, pict, s->mbskip_table, &s->low_delay,
  1965. s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
  1966. }
  1967. int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
  1968. {
  1969. AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
  1970. int offset = 2*s->mb_stride + 1;
  1971. if(!ref)
  1972. return AVERROR(ENOMEM);
  1973. av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
  1974. ref->size -= offset;
  1975. ref->data += offset;
  1976. return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
  1977. }
  1978. static inline int hpel_motion_lowres(MpegEncContext *s,
  1979. uint8_t *dest, uint8_t *src,
  1980. int field_based, int field_select,
  1981. int src_x, int src_y,
  1982. int width, int height, ptrdiff_t stride,
  1983. int h_edge_pos, int v_edge_pos,
  1984. int w, int h, h264_chroma_mc_func *pix_op,
  1985. int motion_x, int motion_y)
  1986. {
  1987. const int lowres = s->avctx->lowres;
  1988. const int op_index = FFMIN(lowres, 3);
  1989. const int s_mask = (2 << lowres) - 1;
  1990. int emu = 0;
  1991. int sx, sy;
  1992. if (s->quarter_sample) {
  1993. motion_x /= 2;
  1994. motion_y /= 2;
  1995. }
  1996. sx = motion_x & s_mask;
  1997. sy = motion_y & s_mask;
  1998. src_x += motion_x >> lowres + 1;
  1999. src_y += motion_y >> lowres + 1;
  2000. src += src_y * stride + src_x;
  2001. if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
  2002. (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  2003. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
  2004. s->linesize, s->linesize,
  2005. w + 1, (h + 1) << field_based,
  2006. src_x, src_y << field_based,
  2007. h_edge_pos, v_edge_pos);
  2008. src = s->edge_emu_buffer;
  2009. emu = 1;
  2010. }
  2011. sx = (sx << 2) >> lowres;
  2012. sy = (sy << 2) >> lowres;
  2013. if (field_select)
  2014. src += s->linesize;
  2015. pix_op[op_index](dest, src, stride, h, sx, sy);
  2016. return emu;
  2017. }
  2018. /* apply one mpeg motion vector to the three components */
  2019. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  2020. uint8_t *dest_y,
  2021. uint8_t *dest_cb,
  2022. uint8_t *dest_cr,
  2023. int field_based,
  2024. int bottom_field,
  2025. int field_select,
  2026. uint8_t **ref_picture,
  2027. h264_chroma_mc_func *pix_op,
  2028. int motion_x, int motion_y,
  2029. int h, int mb_y)
  2030. {
  2031. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2032. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
  2033. ptrdiff_t uvlinesize, linesize;
  2034. const int lowres = s->avctx->lowres;
  2035. const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 3);
  2036. const int block_s = 8>>lowres;
  2037. const int s_mask = (2 << lowres) - 1;
  2038. const int h_edge_pos = s->h_edge_pos >> lowres;
  2039. const int v_edge_pos = s->v_edge_pos >> lowres;
  2040. linesize = s->current_picture.f.linesize[0] << field_based;
  2041. uvlinesize = s->current_picture.f.linesize[1] << field_based;
  2042. // FIXME obviously not perfect but qpel will not work in lowres anyway
  2043. if (s->quarter_sample) {
  2044. motion_x /= 2;
  2045. motion_y /= 2;
  2046. }
  2047. if(field_based){
  2048. motion_y += (bottom_field - field_select)*((1 << lowres)-1);
  2049. }
  2050. sx = motion_x & s_mask;
  2051. sy = motion_y & s_mask;
  2052. src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
  2053. src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
  2054. if (s->out_format == FMT_H263) {
  2055. uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
  2056. uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
  2057. uvsrc_x = src_x >> 1;
  2058. uvsrc_y = src_y >> 1;
  2059. } else if (s->out_format == FMT_H261) {
  2060. // even chroma mv's are full pel in H261
  2061. mx = motion_x / 4;
  2062. my = motion_y / 4;
  2063. uvsx = (2 * mx) & s_mask;
  2064. uvsy = (2 * my) & s_mask;
  2065. uvsrc_x = s->mb_x * block_s + (mx >> lowres);
  2066. uvsrc_y = mb_y * block_s + (my >> lowres);
  2067. } else {
  2068. if(s->chroma_y_shift){
  2069. mx = motion_x / 2;
  2070. my = motion_y / 2;
  2071. uvsx = mx & s_mask;
  2072. uvsy = my & s_mask;
  2073. uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
  2074. uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
  2075. } else {
  2076. if(s->chroma_x_shift){
  2077. //Chroma422
  2078. mx = motion_x / 2;
  2079. uvsx = mx & s_mask;
  2080. uvsy = motion_y & s_mask;
  2081. uvsrc_y = src_y;
  2082. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  2083. } else {
  2084. //Chroma444
  2085. uvsx = motion_x & s_mask;
  2086. uvsy = motion_y & s_mask;
  2087. uvsrc_x = src_x;
  2088. uvsrc_y = src_y;
  2089. }
  2090. }
  2091. }
  2092. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  2093. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  2094. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  2095. if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
  2096. (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
  2097. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
  2098. linesize >> field_based, linesize >> field_based,
  2099. 17, 17 + field_based,
  2100. src_x, src_y << field_based, h_edge_pos,
  2101. v_edge_pos);
  2102. ptr_y = s->edge_emu_buffer;
  2103. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  2104. uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
  2105. s->vdsp.emulated_edge_mc(uvbuf, ptr_cb,
  2106. uvlinesize >> field_based, uvlinesize >> field_based,
  2107. 9, 9 + field_based,
  2108. uvsrc_x, uvsrc_y << field_based,
  2109. h_edge_pos >> 1, v_edge_pos >> 1);
  2110. s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr,
  2111. uvlinesize >> field_based,uvlinesize >> field_based,
  2112. 9, 9 + field_based,
  2113. uvsrc_x, uvsrc_y << field_based,
  2114. h_edge_pos >> 1, v_edge_pos >> 1);
  2115. ptr_cb = uvbuf;
  2116. ptr_cr = uvbuf + 16;
  2117. }
  2118. }
  2119. // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
  2120. if (bottom_field) {
  2121. dest_y += s->linesize;
  2122. dest_cb += s->uvlinesize;
  2123. dest_cr += s->uvlinesize;
  2124. }
  2125. if (field_select) {
  2126. ptr_y += s->linesize;
  2127. ptr_cb += s->uvlinesize;
  2128. ptr_cr += s->uvlinesize;
  2129. }
  2130. sx = (sx << 2) >> lowres;
  2131. sy = (sy << 2) >> lowres;
  2132. pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
  2133. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  2134. int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
  2135. uvsx = (uvsx << 2) >> lowres;
  2136. uvsy = (uvsy << 2) >> lowres;
  2137. if (hc) {
  2138. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
  2139. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
  2140. }
  2141. }
  2142. // FIXME h261 lowres loop filter
  2143. }
  2144. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  2145. uint8_t *dest_cb, uint8_t *dest_cr,
  2146. uint8_t **ref_picture,
  2147. h264_chroma_mc_func * pix_op,
  2148. int mx, int my)
  2149. {
  2150. const int lowres = s->avctx->lowres;
  2151. const int op_index = FFMIN(lowres, 3);
  2152. const int block_s = 8 >> lowres;
  2153. const int s_mask = (2 << lowres) - 1;
  2154. const int h_edge_pos = s->h_edge_pos >> lowres + 1;
  2155. const int v_edge_pos = s->v_edge_pos >> lowres + 1;
  2156. int emu = 0, src_x, src_y, sx, sy;
  2157. ptrdiff_t offset;
  2158. uint8_t *ptr;
  2159. if (s->quarter_sample) {
  2160. mx /= 2;
  2161. my /= 2;
  2162. }
  2163. /* In case of 8X8, we construct a single chroma motion vector
  2164. with a special rounding */
  2165. mx = ff_h263_round_chroma(mx);
  2166. my = ff_h263_round_chroma(my);
  2167. sx = mx & s_mask;
  2168. sy = my & s_mask;
  2169. src_x = s->mb_x * block_s + (mx >> lowres + 1);
  2170. src_y = s->mb_y * block_s + (my >> lowres + 1);
  2171. offset = src_y * s->uvlinesize + src_x;
  2172. ptr = ref_picture[1] + offset;
  2173. if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
  2174. (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
  2175. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  2176. s->uvlinesize, s->uvlinesize,
  2177. 9, 9,
  2178. src_x, src_y, h_edge_pos, v_edge_pos);
  2179. ptr = s->edge_emu_buffer;
  2180. emu = 1;
  2181. }
  2182. sx = (sx << 2) >> lowres;
  2183. sy = (sy << 2) >> lowres;
  2184. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  2185. ptr = ref_picture[2] + offset;
  2186. if (emu) {
  2187. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
  2188. s->uvlinesize, s->uvlinesize,
  2189. 9, 9,
  2190. src_x, src_y, h_edge_pos, v_edge_pos);
  2191. ptr = s->edge_emu_buffer;
  2192. }
  2193. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  2194. }
  2195. /**
  2196. * motion compensation of a single macroblock
  2197. * @param s context
  2198. * @param dest_y luma destination pointer
  2199. * @param dest_cb chroma cb/u destination pointer
  2200. * @param dest_cr chroma cr/v destination pointer
  2201. * @param dir direction (0->forward, 1->backward)
  2202. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  2203. * @param pix_op halfpel motion compensation function (average or put normally)
  2204. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  2205. */
  2206. static inline void MPV_motion_lowres(MpegEncContext *s,
  2207. uint8_t *dest_y, uint8_t *dest_cb,
  2208. uint8_t *dest_cr,
  2209. int dir, uint8_t **ref_picture,
  2210. h264_chroma_mc_func *pix_op)
  2211. {
  2212. int mx, my;
  2213. int mb_x, mb_y, i;
  2214. const int lowres = s->avctx->lowres;
  2215. const int block_s = 8 >>lowres;
  2216. mb_x = s->mb_x;
  2217. mb_y = s->mb_y;
  2218. switch (s->mv_type) {
  2219. case MV_TYPE_16X16:
  2220. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2221. 0, 0, 0,
  2222. ref_picture, pix_op,
  2223. s->mv[dir][0][0], s->mv[dir][0][1],
  2224. 2 * block_s, mb_y);
  2225. break;
  2226. case MV_TYPE_8X8:
  2227. mx = 0;
  2228. my = 0;
  2229. for (i = 0; i < 4; i++) {
  2230. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
  2231. s->linesize) * block_s,
  2232. ref_picture[0], 0, 0,
  2233. (2 * mb_x + (i & 1)) * block_s,
  2234. (2 * mb_y + (i >> 1)) * block_s,
  2235. s->width, s->height, s->linesize,
  2236. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  2237. block_s, block_s, pix_op,
  2238. s->mv[dir][i][0], s->mv[dir][i][1]);
  2239. mx += s->mv[dir][i][0];
  2240. my += s->mv[dir][i][1];
  2241. }
  2242. if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
  2243. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
  2244. pix_op, mx, my);
  2245. break;
  2246. case MV_TYPE_FIELD:
  2247. if (s->picture_structure == PICT_FRAME) {
  2248. /* top field */
  2249. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2250. 1, 0, s->field_select[dir][0],
  2251. ref_picture, pix_op,
  2252. s->mv[dir][0][0], s->mv[dir][0][1],
  2253. block_s, mb_y);
  2254. /* bottom field */
  2255. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2256. 1, 1, s->field_select[dir][1],
  2257. ref_picture, pix_op,
  2258. s->mv[dir][1][0], s->mv[dir][1][1],
  2259. block_s, mb_y);
  2260. } else {
  2261. if (s->picture_structure != s->field_select[dir][0] + 1 &&
  2262. s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
  2263. ref_picture = s->current_picture_ptr->f.data;
  2264. }
  2265. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2266. 0, 0, s->field_select[dir][0],
  2267. ref_picture, pix_op,
  2268. s->mv[dir][0][0],
  2269. s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
  2270. }
  2271. break;
  2272. case MV_TYPE_16X8:
  2273. for (i = 0; i < 2; i++) {
  2274. uint8_t **ref2picture;
  2275. if (s->picture_structure == s->field_select[dir][i] + 1 ||
  2276. s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
  2277. ref2picture = ref_picture;
  2278. } else {
  2279. ref2picture = s->current_picture_ptr->f.data;
  2280. }
  2281. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2282. 0, 0, s->field_select[dir][i],
  2283. ref2picture, pix_op,
  2284. s->mv[dir][i][0], s->mv[dir][i][1] +
  2285. 2 * block_s * i, block_s, mb_y >> 1);
  2286. dest_y += 2 * block_s * s->linesize;
  2287. dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2288. dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
  2289. }
  2290. break;
  2291. case MV_TYPE_DMV:
  2292. if (s->picture_structure == PICT_FRAME) {
  2293. for (i = 0; i < 2; i++) {
  2294. int j;
  2295. for (j = 0; j < 2; j++) {
  2296. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2297. 1, j, j ^ i,
  2298. ref_picture, pix_op,
  2299. s->mv[dir][2 * i + j][0],
  2300. s->mv[dir][2 * i + j][1],
  2301. block_s, mb_y);
  2302. }
  2303. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2304. }
  2305. } else {
  2306. for (i = 0; i < 2; i++) {
  2307. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  2308. 0, 0, s->picture_structure != i + 1,
  2309. ref_picture, pix_op,
  2310. s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
  2311. 2 * block_s, mb_y >> 1);
  2312. // after put we make avg of the same block
  2313. pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
  2314. // opposite parity is always in the same
  2315. // frame if this is second field
  2316. if (!s->first_field) {
  2317. ref_picture = s->current_picture_ptr->f.data;
  2318. }
  2319. }
  2320. }
  2321. break;
  2322. default:
  2323. av_assert2(0);
  2324. }
  2325. }
  2326. /**
  2327. * find the lowest MB row referenced in the MVs
  2328. */
  2329. int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  2330. {
  2331. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  2332. int my, off, i, mvs;
  2333. if (s->picture_structure != PICT_FRAME || s->mcsel)
  2334. goto unhandled;
  2335. switch (s->mv_type) {
  2336. case MV_TYPE_16X16:
  2337. mvs = 1;
  2338. break;
  2339. case MV_TYPE_16X8:
  2340. mvs = 2;
  2341. break;
  2342. case MV_TYPE_8X8:
  2343. mvs = 4;
  2344. break;
  2345. default:
  2346. goto unhandled;
  2347. }
  2348. for (i = 0; i < mvs; i++) {
  2349. my = s->mv[dir][i][1]<<qpel_shift;
  2350. my_max = FFMAX(my_max, my);
  2351. my_min = FFMIN(my_min, my);
  2352. }
  2353. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  2354. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  2355. unhandled:
  2356. return s->mb_height-1;
  2357. }
  2358. /* put block[] to dest[] */
  2359. static inline void put_dct(MpegEncContext *s,
  2360. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2361. {
  2362. s->dct_unquantize_intra(s, block, i, qscale);
  2363. s->dsp.idct_put (dest, line_size, block);
  2364. }
  2365. /* add block[] to dest[] */
  2366. static inline void add_dct(MpegEncContext *s,
  2367. int16_t *block, int i, uint8_t *dest, int line_size)
  2368. {
  2369. if (s->block_last_index[i] >= 0) {
  2370. s->dsp.idct_add (dest, line_size, block);
  2371. }
  2372. }
  2373. static inline void add_dequant_dct(MpegEncContext *s,
  2374. int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
  2375. {
  2376. if (s->block_last_index[i] >= 0) {
  2377. s->dct_unquantize_inter(s, block, i, qscale);
  2378. s->dsp.idct_add (dest, line_size, block);
  2379. }
  2380. }
  2381. /**
  2382. * Clean dc, ac, coded_block for the current non-intra MB.
  2383. */
  2384. void ff_clean_intra_table_entries(MpegEncContext *s)
  2385. {
  2386. int wrap = s->b8_stride;
  2387. int xy = s->block_index[0];
  2388. s->dc_val[0][xy ] =
  2389. s->dc_val[0][xy + 1 ] =
  2390. s->dc_val[0][xy + wrap] =
  2391. s->dc_val[0][xy + 1 + wrap] = 1024;
  2392. /* ac pred */
  2393. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2394. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2395. if (s->msmpeg4_version>=3) {
  2396. s->coded_block[xy ] =
  2397. s->coded_block[xy + 1 ] =
  2398. s->coded_block[xy + wrap] =
  2399. s->coded_block[xy + 1 + wrap] = 0;
  2400. }
  2401. /* chroma */
  2402. wrap = s->mb_stride;
  2403. xy = s->mb_x + s->mb_y * wrap;
  2404. s->dc_val[1][xy] =
  2405. s->dc_val[2][xy] = 1024;
  2406. /* ac pred */
  2407. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2408. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2409. s->mbintra_table[xy]= 0;
  2410. }
  2411. /* generic function called after a macroblock has been parsed by the
  2412. decoder or after it has been encoded by the encoder.
  2413. Important variables used:
  2414. s->mb_intra : true if intra macroblock
  2415. s->mv_dir : motion vector direction
  2416. s->mv_type : motion vector type
  2417. s->mv : motion vector
  2418. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2419. */
  2420. static av_always_inline
  2421. void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
  2422. int lowres_flag, int is_mpeg12)
  2423. {
  2424. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2425. if (CONFIG_XVMC &&
  2426. s->avctx->hwaccel && s->avctx->hwaccel->decode_mb) {
  2427. s->avctx->hwaccel->decode_mb(s);//xvmc uses pblocks
  2428. return;
  2429. }
  2430. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  2431. /* print DCT coefficients */
  2432. int i,j;
  2433. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  2434. for(i=0; i<6; i++){
  2435. for(j=0; j<64; j++){
  2436. av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
  2437. }
  2438. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  2439. }
  2440. }
  2441. s->current_picture.qscale_table[mb_xy] = s->qscale;
  2442. /* update DC predictors for P macroblocks */
  2443. if (!s->mb_intra) {
  2444. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  2445. if(s->mbintra_table[mb_xy])
  2446. ff_clean_intra_table_entries(s);
  2447. } else {
  2448. s->last_dc[0] =
  2449. s->last_dc[1] =
  2450. s->last_dc[2] = 128 << s->intra_dc_precision;
  2451. }
  2452. }
  2453. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  2454. s->mbintra_table[mb_xy]=1;
  2455. if ( (s->flags&CODEC_FLAG_PSNR)
  2456. || s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor
  2457. || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
  2458. uint8_t *dest_y, *dest_cb, *dest_cr;
  2459. int dct_linesize, dct_offset;
  2460. op_pixels_func (*op_pix)[4];
  2461. qpel_mc_func (*op_qpix)[16];
  2462. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2463. const int uvlinesize = s->current_picture.f.linesize[1];
  2464. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  2465. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  2466. /* avoid copy if macroblock skipped in last frame too */
  2467. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2468. if(!s->encoding){
  2469. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2470. if (s->mb_skipped) {
  2471. s->mb_skipped= 0;
  2472. av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
  2473. *mbskip_ptr = 1;
  2474. } else if(!s->current_picture.reference) {
  2475. *mbskip_ptr = 1;
  2476. } else{
  2477. *mbskip_ptr = 0; /* not skipped */
  2478. }
  2479. }
  2480. dct_linesize = linesize << s->interlaced_dct;
  2481. dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
  2482. if(readable){
  2483. dest_y= s->dest[0];
  2484. dest_cb= s->dest[1];
  2485. dest_cr= s->dest[2];
  2486. }else{
  2487. dest_y = s->b_scratchpad;
  2488. dest_cb= s->b_scratchpad+16*linesize;
  2489. dest_cr= s->b_scratchpad+32*linesize;
  2490. }
  2491. if (!s->mb_intra) {
  2492. /* motion handling */
  2493. /* decoding or more than one mb_type (MC was already done otherwise) */
  2494. if(!s->encoding){
  2495. if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  2496. if (s->mv_dir & MV_DIR_FORWARD) {
  2497. ff_thread_await_progress(&s->last_picture_ptr->tf,
  2498. ff_MPV_lowest_referenced_row(s, 0),
  2499. 0);
  2500. }
  2501. if (s->mv_dir & MV_DIR_BACKWARD) {
  2502. ff_thread_await_progress(&s->next_picture_ptr->tf,
  2503. ff_MPV_lowest_referenced_row(s, 1),
  2504. 0);
  2505. }
  2506. }
  2507. if(lowres_flag){
  2508. h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
  2509. if (s->mv_dir & MV_DIR_FORWARD) {
  2510. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
  2511. op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
  2512. }
  2513. if (s->mv_dir & MV_DIR_BACKWARD) {
  2514. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
  2515. }
  2516. }else{
  2517. op_qpix = s->me.qpel_put;
  2518. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  2519. op_pix = s->hdsp.put_pixels_tab;
  2520. }else{
  2521. op_pix = s->hdsp.put_no_rnd_pixels_tab;
  2522. }
  2523. if (s->mv_dir & MV_DIR_FORWARD) {
  2524. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  2525. op_pix = s->hdsp.avg_pixels_tab;
  2526. op_qpix= s->me.qpel_avg;
  2527. }
  2528. if (s->mv_dir & MV_DIR_BACKWARD) {
  2529. ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  2530. }
  2531. }
  2532. }
  2533. /* skip dequant / idct if we are really late ;) */
  2534. if(s->avctx->skip_idct){
  2535. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  2536. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  2537. || s->avctx->skip_idct >= AVDISCARD_ALL)
  2538. goto skip_idct;
  2539. }
  2540. /* add dct residue */
  2541. if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
  2542. || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2543. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2544. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2545. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2546. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2547. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2548. if (s->chroma_y_shift){
  2549. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2550. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2551. }else{
  2552. dct_linesize >>= 1;
  2553. dct_offset >>=1;
  2554. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2555. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2556. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2557. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2558. }
  2559. }
  2560. } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
  2561. add_dct(s, block[0], 0, dest_y , dct_linesize);
  2562. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  2563. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  2564. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  2565. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2566. if(s->chroma_y_shift){//Chroma420
  2567. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2568. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2569. }else{
  2570. //chroma422
  2571. dct_linesize = uvlinesize << s->interlaced_dct;
  2572. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2573. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  2574. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  2575. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  2576. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  2577. if(!s->chroma_x_shift){//Chroma444
  2578. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  2579. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  2580. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  2581. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  2582. }
  2583. }
  2584. }//fi gray
  2585. }
  2586. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  2587. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2588. }
  2589. } else {
  2590. /* dct only in intra block */
  2591. if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
  2592. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  2593. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  2594. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  2595. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  2596. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2597. if(s->chroma_y_shift){
  2598. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2599. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2600. }else{
  2601. dct_offset >>=1;
  2602. dct_linesize >>=1;
  2603. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  2604. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  2605. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  2606. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  2607. }
  2608. }
  2609. }else{
  2610. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  2611. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  2612. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  2613. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  2614. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  2615. if(s->chroma_y_shift){
  2616. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  2617. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  2618. }else{
  2619. dct_linesize = uvlinesize << s->interlaced_dct;
  2620. dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
  2621. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  2622. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  2623. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  2624. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  2625. if(!s->chroma_x_shift){//Chroma444
  2626. s->dsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  2627. s->dsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  2628. s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  2629. s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  2630. }
  2631. }
  2632. }//gray
  2633. }
  2634. }
  2635. skip_idct:
  2636. if(!readable){
  2637. s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2638. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2639. s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2640. }
  2641. }
  2642. }
  2643. void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){
  2644. #if !CONFIG_SMALL
  2645. if(s->out_format == FMT_MPEG1) {
  2646. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  2647. else MPV_decode_mb_internal(s, block, 0, 1);
  2648. } else
  2649. #endif
  2650. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  2651. else MPV_decode_mb_internal(s, block, 0, 0);
  2652. }
  2653. /**
  2654. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2655. */
  2656. void ff_draw_horiz_band(AVCodecContext *avctx, Picture *cur,
  2657. Picture *last, int y, int h, int picture_structure,
  2658. int first_field, int low_delay)
  2659. {
  2660. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  2661. int vshift = desc->log2_chroma_h;
  2662. const int field_pic = picture_structure != PICT_FRAME;
  2663. if(field_pic){
  2664. h <<= 1;
  2665. y <<= 1;
  2666. }
  2667. h = FFMIN(h, avctx->height - y);
  2668. if(field_pic && first_field && !(avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2669. if (avctx->draw_horiz_band) {
  2670. AVFrame *src;
  2671. int offset[AV_NUM_DATA_POINTERS];
  2672. int i;
  2673. if(cur->f.pict_type == AV_PICTURE_TYPE_B || low_delay ||
  2674. (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2675. src = &cur->f;
  2676. else if (last)
  2677. src = &last->f;
  2678. else
  2679. return;
  2680. if (cur->f.pict_type == AV_PICTURE_TYPE_B &&
  2681. picture_structure == PICT_FRAME &&
  2682. avctx->codec_id != AV_CODEC_ID_SVQ3) {
  2683. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  2684. offset[i] = 0;
  2685. }else{
  2686. offset[0]= y * src->linesize[0];
  2687. offset[1]=
  2688. offset[2]= (y >> vshift) * src->linesize[1];
  2689. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  2690. offset[i] = 0;
  2691. }
  2692. emms_c();
  2693. avctx->draw_horiz_band(avctx, src, offset,
  2694. y, picture_structure, h);
  2695. }
  2696. }
  2697. void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
  2698. {
  2699. ff_draw_horiz_band(s->avctx, s->current_picture_ptr,
  2700. s->last_picture_ptr, y, h, s->picture_structure,
  2701. s->first_field, s->low_delay);
  2702. }
  2703. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2704. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2705. const int uvlinesize = s->current_picture.f.linesize[1];
  2706. const int mb_size= 4 - s->avctx->lowres;
  2707. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2708. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2709. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2710. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2711. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2712. 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;
  2713. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2714. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2715. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2716. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2717. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2718. {
  2719. if(s->picture_structure==PICT_FRAME){
  2720. s->dest[0] += s->mb_y * linesize << mb_size;
  2721. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2722. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2723. }else{
  2724. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2725. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2726. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2727. av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2728. }
  2729. }
  2730. }
  2731. /**
  2732. * Permute an 8x8 block.
  2733. * @param block the block which will be permuted according to the given permutation vector
  2734. * @param permutation the permutation vector
  2735. * @param last the last non zero coefficient in scantable order, used to speed the permutation up
  2736. * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
  2737. * (inverse) permutated to scantable order!
  2738. */
  2739. void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
  2740. {
  2741. int i;
  2742. int16_t temp[64];
  2743. if(last<=0) return;
  2744. //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
  2745. for(i=0; i<=last; i++){
  2746. const int j= scantable[i];
  2747. temp[j]= block[j];
  2748. block[j]=0;
  2749. }
  2750. for(i=0; i<=last; i++){
  2751. const int j= scantable[i];
  2752. const int perm_j= permutation[j];
  2753. block[perm_j]= temp[j];
  2754. }
  2755. }
  2756. void ff_mpeg_flush(AVCodecContext *avctx){
  2757. int i;
  2758. MpegEncContext *s = avctx->priv_data;
  2759. if(s==NULL || s->picture==NULL)
  2760. return;
  2761. for (i = 0; i < MAX_PICTURE_COUNT; i++)
  2762. ff_mpeg_unref_picture(s, &s->picture[i]);
  2763. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2764. ff_mpeg_unref_picture(s, &s->current_picture);
  2765. ff_mpeg_unref_picture(s, &s->last_picture);
  2766. ff_mpeg_unref_picture(s, &s->next_picture);
  2767. s->mb_x= s->mb_y= 0;
  2768. s->closed_gop= 0;
  2769. s->parse_context.state= -1;
  2770. s->parse_context.frame_start_found= 0;
  2771. s->parse_context.overread= 0;
  2772. s->parse_context.overread_index= 0;
  2773. s->parse_context.index= 0;
  2774. s->parse_context.last_index= 0;
  2775. s->bitstream_buffer_size=0;
  2776. s->pp_time=0;
  2777. }
  2778. /**
  2779. * set qscale and update qscale dependent variables.
  2780. */
  2781. void ff_set_qscale(MpegEncContext * s, int qscale)
  2782. {
  2783. if (qscale < 1)
  2784. qscale = 1;
  2785. else if (qscale > 31)
  2786. qscale = 31;
  2787. s->qscale = qscale;
  2788. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2789. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2790. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2791. }
  2792. void ff_MPV_report_decode_progress(MpegEncContext *s)
  2793. {
  2794. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
  2795. ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
  2796. }
  2797. #if CONFIG_ERROR_RESILIENCE
  2798. void ff_mpeg_er_frame_start(MpegEncContext *s)
  2799. {
  2800. ERContext *er = &s->er;
  2801. er->cur_pic = s->current_picture_ptr;
  2802. er->last_pic = s->last_picture_ptr;
  2803. er->next_pic = s->next_picture_ptr;
  2804. er->pp_time = s->pp_time;
  2805. er->pb_time = s->pb_time;
  2806. er->quarter_sample = s->quarter_sample;
  2807. er->partitioned_frame = s->partitioned_frame;
  2808. ff_er_frame_start(er);
  2809. }
  2810. #endif /* CONFIG_ERROR_RESILIENCE */