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.

266 lines
11KB

  1. /*
  2. * Mpeg video formats-related defines and utility 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/common.h"
  22. #include "libavutil/frame.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "libavutil/motion_vector.h"
  25. #include "libavutil/avassert.h"
  26. #include "avcodec.h"
  27. #include "mpegutils.h"
  28. static int add_mb(AVMotionVector *mb, uint32_t mb_type,
  29. int dst_x, int dst_y,
  30. int motion_x, int motion_y, int motion_scale,
  31. int direction)
  32. {
  33. mb->w = IS_8X8(mb_type) || IS_8X16(mb_type) ? 8 : 16;
  34. mb->h = IS_8X8(mb_type) || IS_16X8(mb_type) ? 8 : 16;
  35. mb->motion_x = motion_x;
  36. mb->motion_y = motion_y;
  37. mb->motion_scale = motion_scale;
  38. mb->dst_x = dst_x;
  39. mb->dst_y = dst_y;
  40. mb->src_x = dst_x + motion_x / motion_scale;
  41. mb->src_y = dst_y + motion_y / motion_scale;
  42. mb->source = direction ? 1 : -1;
  43. mb->flags = 0; // XXX: does mb_type contain extra information that could be exported here?
  44. return 1;
  45. }
  46. void ff_draw_horiz_band(AVCodecContext *avctx,
  47. AVFrame *cur, AVFrame *last,
  48. int y, int h, int picture_structure,
  49. int first_field, int low_delay)
  50. {
  51. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  52. int vshift = desc->log2_chroma_h;
  53. const int field_pic = picture_structure != PICT_FRAME;
  54. if (field_pic) {
  55. h <<= 1;
  56. y <<= 1;
  57. }
  58. h = FFMIN(h, avctx->height - y);
  59. if (field_pic && first_field &&
  60. !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
  61. return;
  62. if (avctx->draw_horiz_band) {
  63. AVFrame *src;
  64. int offset[AV_NUM_DATA_POINTERS];
  65. int i;
  66. if (cur->pict_type == AV_PICTURE_TYPE_B || low_delay ||
  67. (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  68. src = cur;
  69. else if (last)
  70. src = last;
  71. else
  72. return;
  73. if (cur->pict_type == AV_PICTURE_TYPE_B &&
  74. picture_structure == PICT_FRAME &&
  75. avctx->codec_id != AV_CODEC_ID_SVQ3) {
  76. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  77. offset[i] = 0;
  78. } else {
  79. offset[0]= y * src->linesize[0];
  80. offset[1]=
  81. offset[2]= (y >> vshift) * src->linesize[1];
  82. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  83. offset[i] = 0;
  84. }
  85. emms_c();
  86. avctx->draw_horiz_band(avctx, src, offset,
  87. y, picture_structure, h);
  88. }
  89. }
  90. void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_table,
  91. uint32_t *mbtype_table, int8_t *qscale_table, int16_t (*motion_val[2])[2],
  92. int *low_delay,
  93. int mb_width, int mb_height, int mb_stride, int quarter_sample)
  94. {
  95. if ((avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS) && mbtype_table && motion_val[0]) {
  96. const int shift = 1 + quarter_sample;
  97. const int scale = 1 << shift;
  98. const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
  99. const int mv_stride = (mb_width << mv_sample_log2) +
  100. (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
  101. int mb_x, mb_y, mbcount = 0;
  102. /* size is width * height * 2 * 4 where 2 is for directions and 4 is
  103. * for the maximum number of MB (4 MB in case of IS_8x8) */
  104. AVMotionVector *mvs = av_malloc_array(mb_width * mb_height, 2 * 4 * sizeof(AVMotionVector));
  105. if (!mvs)
  106. return;
  107. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  108. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  109. int i, direction, mb_type = mbtype_table[mb_x + mb_y * mb_stride];
  110. for (direction = 0; direction < 2; direction++) {
  111. if (!USES_LIST(mb_type, direction))
  112. continue;
  113. if (IS_8X8(mb_type)) {
  114. for (i = 0; i < 4; i++) {
  115. int sx = mb_x * 16 + 4 + 8 * (i & 1);
  116. int sy = mb_y * 16 + 4 + 8 * (i >> 1);
  117. int xy = (mb_x * 2 + (i & 1) +
  118. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  119. int mx = motion_val[direction][xy][0];
  120. int my = motion_val[direction][xy][1];
  121. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
  122. }
  123. } else if (IS_16X8(mb_type)) {
  124. for (i = 0; i < 2; i++) {
  125. int sx = mb_x * 16 + 8;
  126. int sy = mb_y * 16 + 4 + 8 * i;
  127. int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
  128. int mx = motion_val[direction][xy][0];
  129. int my = motion_val[direction][xy][1];
  130. if (IS_INTERLACED(mb_type))
  131. my *= 2;
  132. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
  133. }
  134. } else if (IS_8X16(mb_type)) {
  135. for (i = 0; i < 2; i++) {
  136. int sx = mb_x * 16 + 4 + 8 * i;
  137. int sy = mb_y * 16 + 8;
  138. int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
  139. int mx = motion_val[direction][xy][0];
  140. int my = motion_val[direction][xy][1];
  141. if (IS_INTERLACED(mb_type))
  142. my *= 2;
  143. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
  144. }
  145. } else {
  146. int sx = mb_x * 16 + 8;
  147. int sy = mb_y * 16 + 8;
  148. int xy = (mb_x + mb_y * mv_stride) << mv_sample_log2;
  149. int mx = motion_val[direction][xy][0];
  150. int my = motion_val[direction][xy][1];
  151. mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
  152. }
  153. }
  154. }
  155. }
  156. if (mbcount) {
  157. AVFrameSideData *sd;
  158. av_log(avctx, AV_LOG_DEBUG, "Adding %d MVs info to frame %d\n", mbcount, avctx->frame_number);
  159. sd = av_frame_new_side_data(pict, AV_FRAME_DATA_MOTION_VECTORS, mbcount * sizeof(AVMotionVector));
  160. if (!sd) {
  161. av_freep(&mvs);
  162. return;
  163. }
  164. memcpy(sd->data, mvs, mbcount * sizeof(AVMotionVector));
  165. }
  166. av_freep(&mvs);
  167. }
  168. /* TODO: export all the following to make them accessible for users (and filters) */
  169. if (avctx->hwaccel || !mbtype_table)
  170. return;
  171. if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
  172. int x,y;
  173. av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
  174. av_get_picture_type_char(pict->pict_type));
  175. for (y = 0; y < mb_height; y++) {
  176. for (x = 0; x < mb_width; x++) {
  177. if (avctx->debug & FF_DEBUG_SKIP) {
  178. int count = mbskip_table ? mbskip_table[x + y * mb_stride] : 0;
  179. if (count > 9)
  180. count = 9;
  181. av_log(avctx, AV_LOG_DEBUG, "%1d", count);
  182. }
  183. if (avctx->debug & FF_DEBUG_QP) {
  184. av_log(avctx, AV_LOG_DEBUG, "%2d",
  185. qscale_table[x + y * mb_stride]);
  186. }
  187. if (avctx->debug & FF_DEBUG_MB_TYPE) {
  188. int mb_type = mbtype_table[x + y * mb_stride];
  189. // Type & MV direction
  190. if (IS_PCM(mb_type))
  191. av_log(avctx, AV_LOG_DEBUG, "P");
  192. else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  193. av_log(avctx, AV_LOG_DEBUG, "A");
  194. else if (IS_INTRA4x4(mb_type))
  195. av_log(avctx, AV_LOG_DEBUG, "i");
  196. else if (IS_INTRA16x16(mb_type))
  197. av_log(avctx, AV_LOG_DEBUG, "I");
  198. else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  199. av_log(avctx, AV_LOG_DEBUG, "d");
  200. else if (IS_DIRECT(mb_type))
  201. av_log(avctx, AV_LOG_DEBUG, "D");
  202. else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
  203. av_log(avctx, AV_LOG_DEBUG, "g");
  204. else if (IS_GMC(mb_type))
  205. av_log(avctx, AV_LOG_DEBUG, "G");
  206. else if (IS_SKIP(mb_type))
  207. av_log(avctx, AV_LOG_DEBUG, "S");
  208. else if (!USES_LIST(mb_type, 1))
  209. av_log(avctx, AV_LOG_DEBUG, ">");
  210. else if (!USES_LIST(mb_type, 0))
  211. av_log(avctx, AV_LOG_DEBUG, "<");
  212. else {
  213. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  214. av_log(avctx, AV_LOG_DEBUG, "X");
  215. }
  216. // segmentation
  217. if (IS_8X8(mb_type))
  218. av_log(avctx, AV_LOG_DEBUG, "+");
  219. else if (IS_16X8(mb_type))
  220. av_log(avctx, AV_LOG_DEBUG, "-");
  221. else if (IS_8X16(mb_type))
  222. av_log(avctx, AV_LOG_DEBUG, "|");
  223. else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
  224. av_log(avctx, AV_LOG_DEBUG, " ");
  225. else
  226. av_log(avctx, AV_LOG_DEBUG, "?");
  227. if (IS_INTERLACED(mb_type))
  228. av_log(avctx, AV_LOG_DEBUG, "=");
  229. else
  230. av_log(avctx, AV_LOG_DEBUG, " ");
  231. }
  232. }
  233. av_log(avctx, AV_LOG_DEBUG, "\n");
  234. }
  235. }
  236. }