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.

2569 lines
88KB

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