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.

467 lines
15KB

  1. /*
  2. * Mpeg video formats-related picture management functions
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdint.h>
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/common.h"
  23. #include "avcodec.h"
  24. #include "motion_est.h"
  25. #include "mpegpicture.h"
  26. #include "mpegutils.h"
  27. static int make_tables_writable(Picture *pic)
  28. {
  29. int ret, i;
  30. #define MAKE_WRITABLE(table) \
  31. do {\
  32. if (pic->table &&\
  33. (ret = av_buffer_make_writable(&pic->table)) < 0)\
  34. return ret;\
  35. } while (0)
  36. MAKE_WRITABLE(mb_var_buf);
  37. MAKE_WRITABLE(mc_mb_var_buf);
  38. MAKE_WRITABLE(mb_mean_buf);
  39. MAKE_WRITABLE(mbskip_table_buf);
  40. MAKE_WRITABLE(qscale_table_buf);
  41. MAKE_WRITABLE(mb_type_buf);
  42. for (i = 0; i < 2; i++) {
  43. MAKE_WRITABLE(motion_val_buf[i]);
  44. MAKE_WRITABLE(ref_index_buf[i]);
  45. }
  46. return 0;
  47. }
  48. int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
  49. ScratchpadContext *sc, int linesize)
  50. {
  51. int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
  52. if (avctx->hwaccel || avctx->codec->capabilities & AV_CODEC_CAP_HWACCEL_VDPAU)
  53. return 0;
  54. if (linesize < 24) {
  55. av_log(avctx, AV_LOG_ERROR, "Image too small, temporary buffers cannot function\n");
  56. return AVERROR_PATCHWELCOME;
  57. }
  58. // edge emu needs blocksize + filter length - 1
  59. // (= 17x17 for halfpel / 21x21 for h264)
  60. // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
  61. // at uvlinesize. It supports only YUV420 so 24x24 is enough
  62. // linesize * interlaced * MBsize
  63. // we also use this buffer for encoding in encode_mb_internal() needig an additional 32 lines
  64. FF_ALLOCZ_ARRAY_OR_GOTO(avctx, sc->edge_emu_buffer, alloc_size, 4 * 68,
  65. fail);
  66. FF_ALLOCZ_ARRAY_OR_GOTO(avctx, me->scratchpad, alloc_size, 4 * 16 * 2,
  67. fail)
  68. me->temp = me->scratchpad;
  69. sc->rd_scratchpad = me->scratchpad;
  70. sc->b_scratchpad = me->scratchpad;
  71. sc->obmc_scratchpad = me->scratchpad + 16;
  72. return 0;
  73. fail:
  74. av_freep(&sc->edge_emu_buffer);
  75. return AVERROR(ENOMEM);
  76. }
  77. /**
  78. * Allocate a frame buffer
  79. */
  80. static int alloc_frame_buffer(AVCodecContext *avctx, Picture *pic,
  81. MotionEstContext *me, ScratchpadContext *sc,
  82. int chroma_x_shift, int chroma_y_shift,
  83. int linesize, int uvlinesize)
  84. {
  85. int edges_needed = av_codec_is_encoder(avctx->codec);
  86. int r, ret;
  87. pic->tf.f = pic->f;
  88. if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  89. avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
  90. avctx->codec_id != AV_CODEC_ID_MSS2) {
  91. if (edges_needed) {
  92. pic->f->width = avctx->width + 2 * EDGE_WIDTH;
  93. pic->f->height = avctx->height + 2 * EDGE_WIDTH;
  94. }
  95. r = ff_thread_get_buffer(avctx, &pic->tf,
  96. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  97. } else {
  98. pic->f->width = avctx->width;
  99. pic->f->height = avctx->height;
  100. pic->f->format = avctx->pix_fmt;
  101. r = avcodec_default_get_buffer2(avctx, pic->f, 0);
  102. }
  103. if (r < 0 || !pic->f->buf[0]) {
  104. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
  105. r, pic->f->data[0]);
  106. return -1;
  107. }
  108. if (edges_needed) {
  109. int i;
  110. for (i = 0; pic->f->data[i]; i++) {
  111. int offset = (EDGE_WIDTH >> (i ? chroma_y_shift : 0)) *
  112. pic->f->linesize[i] +
  113. (EDGE_WIDTH >> (i ? chroma_x_shift : 0));
  114. pic->f->data[i] += offset;
  115. }
  116. pic->f->width = avctx->width;
  117. pic->f->height = avctx->height;
  118. }
  119. if (avctx->hwaccel) {
  120. assert(!pic->hwaccel_picture_private);
  121. if (avctx->hwaccel->frame_priv_data_size) {
  122. pic->hwaccel_priv_buf = av_buffer_allocz(avctx->hwaccel->frame_priv_data_size);
  123. if (!pic->hwaccel_priv_buf) {
  124. av_log(avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  125. return -1;
  126. }
  127. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  128. }
  129. }
  130. if (linesize && (linesize != pic->f->linesize[0] ||
  131. uvlinesize != pic->f->linesize[1])) {
  132. av_log(avctx, AV_LOG_ERROR,
  133. "get_buffer() failed (stride changed)\n");
  134. ff_mpeg_unref_picture(avctx, pic);
  135. return -1;
  136. }
  137. if (pic->f->linesize[1] != pic->f->linesize[2]) {
  138. av_log(avctx, AV_LOG_ERROR,
  139. "get_buffer() failed (uv stride mismatch)\n");
  140. ff_mpeg_unref_picture(avctx, pic);
  141. return -1;
  142. }
  143. if (!sc->edge_emu_buffer &&
  144. (ret = ff_mpeg_framesize_alloc(avctx, me, sc,
  145. pic->f->linesize[0])) < 0) {
  146. av_log(avctx, AV_LOG_ERROR,
  147. "get_buffer() failed to allocate context scratch buffers.\n");
  148. ff_mpeg_unref_picture(avctx, pic);
  149. return ret;
  150. }
  151. return 0;
  152. }
  153. static int alloc_picture_tables(AVCodecContext *avctx, Picture *pic, int encoding, int out_format,
  154. int mb_stride, int mb_width, int mb_height, int b8_stride)
  155. {
  156. const int big_mb_num = mb_stride * (mb_height + 1) + 1;
  157. const int mb_array_size = mb_stride * mb_height;
  158. const int b8_array_size = b8_stride * mb_height * 2;
  159. int i;
  160. pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
  161. pic->qscale_table_buf = av_buffer_allocz(big_mb_num + mb_stride);
  162. pic->mb_type_buf = av_buffer_allocz((big_mb_num + mb_stride) *
  163. sizeof(uint32_t));
  164. if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
  165. return AVERROR(ENOMEM);
  166. if (encoding) {
  167. pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  168. pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  169. pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
  170. if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
  171. return AVERROR(ENOMEM);
  172. }
  173. if (out_format == FMT_H263 || encoding || avctx->debug_mv ||
  174. (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS)) {
  175. int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
  176. int ref_index_size = 4 * mb_array_size;
  177. for (i = 0; mv_size && i < 2; i++) {
  178. pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
  179. pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
  180. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  181. return AVERROR(ENOMEM);
  182. }
  183. }
  184. pic->alloc_mb_width = mb_width;
  185. pic->alloc_mb_height = mb_height;
  186. return 0;
  187. }
  188. /**
  189. * Allocate a Picture.
  190. * The pixels are allocated/set by calling get_buffer() if shared = 0
  191. */
  192. int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me,
  193. ScratchpadContext *sc, int shared, int encoding,
  194. int chroma_x_shift, int chroma_y_shift, int out_format,
  195. int mb_stride, int mb_width, int mb_height, int b8_stride,
  196. ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
  197. {
  198. int i, ret;
  199. if (pic->qscale_table_buf)
  200. if ( pic->alloc_mb_width != mb_width
  201. || pic->alloc_mb_height != mb_height)
  202. ff_free_picture_tables(pic);
  203. if (shared) {
  204. av_assert0(pic->f->data[0]);
  205. pic->shared = 1;
  206. } else {
  207. av_assert0(!pic->f->buf[0]);
  208. if (alloc_frame_buffer(avctx, pic, me, sc,
  209. chroma_x_shift, chroma_y_shift,
  210. *linesize, *uvlinesize) < 0)
  211. return -1;
  212. *linesize = pic->f->linesize[0];
  213. *uvlinesize = pic->f->linesize[1];
  214. }
  215. if (!pic->qscale_table_buf)
  216. ret = alloc_picture_tables(avctx, pic, encoding, out_format,
  217. mb_stride, mb_width, mb_height, b8_stride);
  218. else
  219. ret = make_tables_writable(pic);
  220. if (ret < 0)
  221. goto fail;
  222. if (encoding) {
  223. pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
  224. pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
  225. pic->mb_mean = pic->mb_mean_buf->data;
  226. }
  227. pic->mbskip_table = pic->mbskip_table_buf->data;
  228. pic->qscale_table = pic->qscale_table_buf->data + 2 * mb_stride + 1;
  229. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * mb_stride + 1;
  230. if (pic->motion_val_buf[0]) {
  231. for (i = 0; i < 2; i++) {
  232. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  233. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  234. }
  235. }
  236. return 0;
  237. fail:
  238. av_log(avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
  239. ff_mpeg_unref_picture(avctx, pic);
  240. ff_free_picture_tables(pic);
  241. return AVERROR(ENOMEM);
  242. }
  243. /**
  244. * Deallocate a picture.
  245. */
  246. void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
  247. {
  248. int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
  249. pic->tf.f = pic->f;
  250. /* WM Image / Screen codecs allocate internal buffers with different
  251. * dimensions / colorspaces; ignore user-defined callbacks for these. */
  252. if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  253. avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
  254. avctx->codec_id != AV_CODEC_ID_MSS2)
  255. ff_thread_release_buffer(avctx, &pic->tf);
  256. else if (pic->f)
  257. av_frame_unref(pic->f);
  258. av_buffer_unref(&pic->hwaccel_priv_buf);
  259. if (pic->needs_realloc)
  260. ff_free_picture_tables(pic);
  261. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  262. }
  263. int ff_update_picture_tables(Picture *dst, Picture *src)
  264. {
  265. int i;
  266. #define UPDATE_TABLE(table) \
  267. do { \
  268. if (src->table && \
  269. (!dst->table || dst->table->buffer != src->table->buffer)) { \
  270. av_buffer_unref(&dst->table); \
  271. dst->table = av_buffer_ref(src->table); \
  272. if (!dst->table) { \
  273. ff_free_picture_tables(dst); \
  274. return AVERROR(ENOMEM); \
  275. } \
  276. } \
  277. } while (0)
  278. UPDATE_TABLE(mb_var_buf);
  279. UPDATE_TABLE(mc_mb_var_buf);
  280. UPDATE_TABLE(mb_mean_buf);
  281. UPDATE_TABLE(mbskip_table_buf);
  282. UPDATE_TABLE(qscale_table_buf);
  283. UPDATE_TABLE(mb_type_buf);
  284. for (i = 0; i < 2; i++) {
  285. UPDATE_TABLE(motion_val_buf[i]);
  286. UPDATE_TABLE(ref_index_buf[i]);
  287. }
  288. dst->mb_var = src->mb_var;
  289. dst->mc_mb_var = src->mc_mb_var;
  290. dst->mb_mean = src->mb_mean;
  291. dst->mbskip_table = src->mbskip_table;
  292. dst->qscale_table = src->qscale_table;
  293. dst->mb_type = src->mb_type;
  294. for (i = 0; i < 2; i++) {
  295. dst->motion_val[i] = src->motion_val[i];
  296. dst->ref_index[i] = src->ref_index[i];
  297. }
  298. dst->alloc_mb_width = src->alloc_mb_width;
  299. dst->alloc_mb_height = src->alloc_mb_height;
  300. return 0;
  301. }
  302. int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
  303. {
  304. int ret;
  305. av_assert0(!dst->f->buf[0]);
  306. av_assert0(src->f->buf[0]);
  307. src->tf.f = src->f;
  308. dst->tf.f = dst->f;
  309. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  310. if (ret < 0)
  311. goto fail;
  312. ret = ff_update_picture_tables(dst, src);
  313. if (ret < 0)
  314. goto fail;
  315. if (src->hwaccel_picture_private) {
  316. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  317. if (!dst->hwaccel_priv_buf)
  318. goto fail;
  319. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  320. }
  321. dst->field_picture = src->field_picture;
  322. dst->mb_var_sum = src->mb_var_sum;
  323. dst->mc_mb_var_sum = src->mc_mb_var_sum;
  324. dst->b_frame_score = src->b_frame_score;
  325. dst->needs_realloc = src->needs_realloc;
  326. dst->reference = src->reference;
  327. dst->shared = src->shared;
  328. return 0;
  329. fail:
  330. ff_mpeg_unref_picture(avctx, dst);
  331. return ret;
  332. }
  333. static inline int pic_is_unused(Picture *pic)
  334. {
  335. if (!pic->f->buf[0])
  336. return 1;
  337. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  338. return 1;
  339. return 0;
  340. }
  341. static int find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
  342. {
  343. int i;
  344. if (shared) {
  345. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  346. if (!picture[i].f->buf[0])
  347. return i;
  348. }
  349. } else {
  350. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  351. if (pic_is_unused(&picture[i]))
  352. return i;
  353. }
  354. }
  355. av_log(avctx, AV_LOG_FATAL,
  356. "Internal error, picture buffer overflow\n");
  357. /* We could return -1, but the codec would crash trying to draw into a
  358. * non-existing frame anyway. This is safer than waiting for a random crash.
  359. * Also the return of this is never useful, an encoder must only allocate
  360. * as much as allowed in the specification. This has no relationship to how
  361. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  362. * enough for such valid streams).
  363. * Plus, a decoder has to check stream validity and remove frames if too
  364. * many reference frames are around. Waiting for "OOM" is not correct at
  365. * all. Similarly, missing reference frames have to be replaced by
  366. * interpolated/MC frames, anything else is a bug in the codec ...
  367. */
  368. abort();
  369. return -1;
  370. }
  371. int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
  372. {
  373. int ret = find_unused_picture(avctx, picture, shared);
  374. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  375. if (picture[ret].needs_realloc) {
  376. picture[ret].needs_realloc = 0;
  377. ff_free_picture_tables(&picture[ret]);
  378. ff_mpeg_unref_picture(avctx, &picture[ret]);
  379. }
  380. }
  381. return ret;
  382. }
  383. void ff_free_picture_tables(Picture *pic)
  384. {
  385. int i;
  386. pic->alloc_mb_width =
  387. pic->alloc_mb_height = 0;
  388. av_buffer_unref(&pic->mb_var_buf);
  389. av_buffer_unref(&pic->mc_mb_var_buf);
  390. av_buffer_unref(&pic->mb_mean_buf);
  391. av_buffer_unref(&pic->mbskip_table_buf);
  392. av_buffer_unref(&pic->qscale_table_buf);
  393. av_buffer_unref(&pic->mb_type_buf);
  394. for (i = 0; i < 2; i++) {
  395. av_buffer_unref(&pic->motion_val_buf[i]);
  396. av_buffer_unref(&pic->ref_index_buf[i]);
  397. }
  398. }