2
0

mpegutils.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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->flags2 & AV_CODEC_FLAG2_EXPORT_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. #if FF_API_DEBUG_MV
  237. if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
  238. (avctx->debug_mv)) {
  239. int mb_y;
  240. int i, ret;
  241. int h_chroma_shift, v_chroma_shift, block_height;
  242. const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
  243. const int mv_stride = (mb_width << mv_sample_log2) +
  244. (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
  245. if (low_delay)
  246. *low_delay = 0; // needed to see the vectors without trashing the buffers
  247. ret = av_pix_fmt_get_chroma_sub_sample (avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  248. if (ret)
  249. return ret;
  250. av_frame_make_writable(pict);
  251. pict->opaque = NULL;
  252. block_height = 16 >> v_chroma_shift;
  253. for (mb_y = 0; mb_y < mb_height; mb_y++) {
  254. int mb_x;
  255. for (mb_x = 0; mb_x < mb_width; mb_x++) {
  256. const int mb_index = mb_x + mb_y * mb_stride;
  257. if ((avctx->debug & FF_DEBUG_VIS_QP)) {
  258. uint64_t c = (qscale_table[mb_index] * 128 / 31) *
  259. 0x0101010101010101ULL;
  260. int y;
  261. for (y = 0; y < block_height; y++) {
  262. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  263. (block_height * mb_y + y) *
  264. pict->linesize[1]) = c;
  265. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  266. (block_height * mb_y + y) *
  267. pict->linesize[2]) = c;
  268. }
  269. }
  270. if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
  271. motion_val[0]) {
  272. int mb_type = mbtype_table[mb_index];
  273. uint64_t u,v;
  274. int y;
  275. #define COLOR(theta, r) \
  276. u = (int)(128 + r * cos(theta * M_PI / 180)); \
  277. v = (int)(128 + r * sin(theta * M_PI / 180));
  278. u = v = 128;
  279. if (IS_PCM(mb_type)) {
  280. COLOR(120, 48)
  281. } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
  282. IS_INTRA16x16(mb_type)) {
  283. COLOR(30, 48)
  284. } else if (IS_INTRA4x4(mb_type)) {
  285. COLOR(90, 48)
  286. } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
  287. // COLOR(120, 48)
  288. } else if (IS_DIRECT(mb_type)) {
  289. COLOR(150, 48)
  290. } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
  291. COLOR(170, 48)
  292. } else if (IS_GMC(mb_type)) {
  293. COLOR(190, 48)
  294. } else if (IS_SKIP(mb_type)) {
  295. // COLOR(180, 48)
  296. } else if (!USES_LIST(mb_type, 1)) {
  297. COLOR(240, 48)
  298. } else if (!USES_LIST(mb_type, 0)) {
  299. COLOR(0, 48)
  300. } else {
  301. av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  302. COLOR(300,48)
  303. }
  304. u *= 0x0101010101010101ULL;
  305. v *= 0x0101010101010101ULL;
  306. for (y = 0; y < block_height; y++) {
  307. *(uint64_t *)(pict->data[1] + 8 * mb_x +
  308. (block_height * mb_y + y) * pict->linesize[1]) = u;
  309. *(uint64_t *)(pict->data[2] + 8 * mb_x +
  310. (block_height * mb_y + y) * pict->linesize[2]) = v;
  311. }
  312. // segmentation
  313. if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
  314. *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
  315. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  316. *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
  317. (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
  318. }
  319. if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
  320. for (y = 0; y < 16; y++)
  321. pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
  322. pict->linesize[0]] ^= 0x80;
  323. }
  324. if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
  325. int dm = 1 << (mv_sample_log2 - 2);
  326. for (i = 0; i < 4; i++) {
  327. int sx = mb_x * 16 + 8 * (i & 1);
  328. int sy = mb_y * 16 + 8 * (i >> 1);
  329. int xy = (mb_x * 2 + (i & 1) +
  330. (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
  331. // FIXME bidir
  332. int32_t *mv = (int32_t *) &motion_val[0][xy];
  333. if (mv[0] != mv[dm] ||
  334. mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
  335. for (y = 0; y < 8; y++)
  336. pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
  337. if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
  338. *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
  339. pict->linesize[0]) ^= 0x8080808080808080ULL;
  340. }
  341. }
  342. if (IS_INTERLACED(mb_type) &&
  343. avctx->codec->id == AV_CODEC_ID_H264) {
  344. // hmm
  345. }
  346. }
  347. if (mbskip_table)
  348. mbskip_table[mb_index] = 0;
  349. }
  350. }
  351. }
  352. #endif
  353. }