scale_slice_test.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdio.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include "decode_simple.h"
  22. #include "libavutil/common.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "libavutil/error.h"
  25. #include "libavutil/lfg.h"
  26. #include "libavutil/random_seed.h"
  27. #include "libavutil/video_enc_params.h"
  28. #include "libavformat/avformat.h"
  29. #include "libavcodec/avcodec.h"
  30. #include "libswscale/swscale.h"
  31. typedef struct PrivData {
  32. unsigned int random_seed;
  33. AVLFG lfg;
  34. struct SwsContext *scaler;
  35. int v_shift_dst, h_shift_dst;
  36. int v_shift_src, h_shift_src;
  37. AVFrame *frame_ref;
  38. AVFrame *frame_dst;
  39. } PrivData;
  40. static int process_frame(DecodeContext *dc, AVFrame *frame)
  41. {
  42. PrivData *pd = dc->opaque;
  43. int slice_start = 0;
  44. int ret;
  45. if (!frame)
  46. return 0;
  47. if (!pd->scaler) {
  48. pd->scaler = sws_getContext(frame->width, frame->height, frame->format,
  49. pd->frame_ref->width, pd->frame_ref->height,
  50. pd->frame_ref->format, 0, NULL, NULL, NULL);
  51. if (!pd->scaler)
  52. return AVERROR(ENOMEM);
  53. av_pix_fmt_get_chroma_sub_sample(frame->format, &pd->h_shift_src, &pd->v_shift_src);
  54. }
  55. /* scale the whole input frame as reference */
  56. ret = sws_scale(pd->scaler, (const uint8_t **)frame->data, frame->linesize, 0, frame->height,
  57. pd->frame_ref->data, pd->frame_ref->linesize);
  58. if (ret < 0)
  59. return ret;
  60. /* scale slices with randomly generated heights */
  61. while (slice_start < frame->height) {
  62. int slice_height;
  63. const uint8_t *src[4];
  64. slice_height = av_lfg_get(&pd->lfg) % (frame->height - slice_start);
  65. slice_height = FFALIGN(FFMAX(1, slice_height), 1 << pd->v_shift_src);
  66. for (int j = 0; j < FF_ARRAY_ELEMS(src) && frame->data[j]; j++) {
  67. int shift = (j == 1 || j == 2) ? pd->v_shift_src : 0;
  68. src[j] = frame->data[j] + frame->linesize[j] * (slice_start >> shift);
  69. }
  70. ret = sws_scale(pd->scaler, src, frame->linesize, slice_start, slice_height,
  71. pd->frame_dst->data, pd->frame_dst->linesize);
  72. if (ret < 0)
  73. return ret;
  74. slice_start += slice_height;
  75. }
  76. /* compare the two results */
  77. for (int i = 0; i < 4 && pd->frame_ref->data[i]; i++) {
  78. int shift = (i == 1 || i == 2) ? pd->v_shift_dst : 0;
  79. if (memcmp(pd->frame_ref->data[i], pd->frame_dst->data[i],
  80. pd->frame_ref->linesize[i] * (pd->frame_ref->height >> shift))) {
  81. fprintf(stderr, "mismatch frame %"PRId64" seed %u\n",
  82. dc->decoder->frame_num - 1, pd->random_seed);
  83. return AVERROR(EINVAL);
  84. }
  85. }
  86. return 0;
  87. }
  88. int main(int argc, char **argv)
  89. {
  90. PrivData pd;
  91. DecodeContext dc;
  92. int width, height;
  93. enum AVPixelFormat pix_fmt;
  94. const char *filename;
  95. int ret = 0;
  96. if (argc <= 4) {
  97. fprintf(stderr,
  98. "Usage: %s <input file> <dst width> <dst height> <dst pixfmt> [<random seed>] \n",
  99. argv[0]);
  100. return 0;
  101. }
  102. memset(&pd, 0, sizeof(pd));
  103. filename = argv[1];
  104. width = strtol(argv[2], NULL, 0);
  105. height = strtol(argv[3], NULL, 0);
  106. pix_fmt = av_get_pix_fmt(argv[4]);
  107. /* init RNG for generating slice sizes */
  108. if (argc >= 6)
  109. pd.random_seed = strtoul(argv[5], NULL, 0);
  110. else
  111. pd.random_seed = av_get_random_seed();
  112. av_lfg_init(&pd.lfg, pd.random_seed);
  113. av_pix_fmt_get_chroma_sub_sample(pix_fmt, &pd.h_shift_dst, &pd.v_shift_dst);
  114. /* allocate the frames for scaler output */
  115. for (int i = 0; i < 2; i++) {
  116. AVFrame *frame = av_frame_alloc();
  117. if (!frame) {
  118. fprintf(stderr, "Error allocating frames\n");
  119. return AVERROR(ENOMEM);
  120. }
  121. frame->width = width;
  122. frame->height = height;
  123. frame->format = pix_fmt;
  124. ret = av_frame_get_buffer(frame, 0);
  125. if (ret < 0) {
  126. fprintf(stderr, "Error allocating frame data\n");
  127. return ret;
  128. }
  129. /* make sure the padding is zeroed */
  130. for (int j = 0; j < 4 && frame->data[j]; j++) {
  131. int shift = (j == 1 || j == 2) ? pd.v_shift_dst : 0;
  132. memset(frame->data[j], 0,
  133. frame->linesize[j] * (height >> shift));
  134. }
  135. if (i) pd.frame_ref = frame;
  136. else pd.frame_dst = frame;
  137. }
  138. ret = ds_open(&dc, filename, 0);
  139. if (ret < 0) {
  140. fprintf(stderr, "Error opening the file\n");
  141. return ret;
  142. }
  143. dc.process_frame = process_frame;
  144. dc.opaque = &pd;
  145. ret = ds_run(&dc);
  146. av_frame_free(&pd.frame_dst);
  147. av_frame_free(&pd.frame_ref);
  148. sws_freeContext(pd.scaler);
  149. ds_free(&dc);
  150. return ret;
  151. }