api-band-test.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2015 Ludmila Glinskih
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /**
  23. * draw_horiz_band test.
  24. */
  25. #include "libavutil/adler32.h"
  26. #include "libavcodec/avcodec.h"
  27. #include "libavformat/avformat.h"
  28. #include "libavutil/imgutils.h"
  29. uint8_t *slice_byte_buffer;
  30. uint8_t slice_byte_buffer_size;
  31. int draw_horiz_band_called;
  32. static void draw_horiz_band(AVCodecContext *ctx, const AVFrame *fr, int offset[4],
  33. int slice_position, int type, int height)
  34. {
  35. int i;
  36. const AVPixFmtDescriptor *pix_fmt_desc;
  37. int chroma_w, chroma_h;
  38. int shift_slice_position;
  39. int shift_height;
  40. draw_horiz_band_called = 1;
  41. pix_fmt_desc = av_pix_fmt_desc_get(ctx->pix_fmt);
  42. chroma_w = -((-ctx->width) >> pix_fmt_desc->log2_chroma_w);
  43. chroma_h = -((-height) >> pix_fmt_desc->log2_chroma_h);
  44. shift_slice_position = -((-slice_position) >> pix_fmt_desc->log2_chroma_h);
  45. shift_height = -((-ctx->height) >> pix_fmt_desc->log2_chroma_h);
  46. for (i = 0; i < height; i++) {
  47. memcpy(slice_byte_buffer + ctx->width * slice_position + i * ctx->width,
  48. fr->data[0] + offset[0] + i * fr->linesize[0], ctx->width);
  49. }
  50. for (i = 0; i < chroma_h; i++) {
  51. memcpy(slice_byte_buffer + ctx->width * ctx->height + chroma_w * shift_slice_position + i * chroma_w,
  52. fr->data[1] + offset[1] + i * fr->linesize[1], chroma_w);
  53. }
  54. for (i = 0; i < chroma_h; i++) {
  55. memcpy(slice_byte_buffer + ctx->width * ctx->height + chroma_w * shift_height + chroma_w * shift_slice_position + i * chroma_w,
  56. fr->data[2] + offset[2] + i * fr->linesize[2], chroma_w);
  57. }
  58. }
  59. static int video_decode(const char *input_filename)
  60. {
  61. AVCodec *codec = NULL;
  62. AVCodecContext *ctx= NULL;
  63. AVCodecParameters *origin_par = NULL;
  64. uint8_t *byte_buffer = NULL;
  65. AVFrame *fr = NULL;
  66. AVPacket pkt;
  67. AVFormatContext *fmt_ctx = NULL;
  68. int number_of_written_bytes;
  69. int video_stream;
  70. int got_frame = 0;
  71. int byte_buffer_size;
  72. int result;
  73. int end_of_stream = 0;
  74. draw_horiz_band_called = 0;
  75. result = avformat_open_input(&fmt_ctx, input_filename, NULL, NULL);
  76. if (result < 0) {
  77. av_log(NULL, AV_LOG_ERROR, "Can't open file\n");
  78. return result;
  79. }
  80. result = avformat_find_stream_info(fmt_ctx, NULL);
  81. if (result < 0) {
  82. av_log(NULL, AV_LOG_ERROR, "Can't get stream info\n");
  83. return result;
  84. }
  85. video_stream = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  86. if (video_stream < 0) {
  87. av_log(NULL, AV_LOG_ERROR, "Can't find video stream in input file\n");
  88. return -1;
  89. }
  90. origin_par = fmt_ctx->streams[video_stream]->codecpar;
  91. codec = avcodec_find_decoder(origin_par->codec_id);
  92. if (!codec) {
  93. av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");
  94. return -1;
  95. }
  96. ctx = avcodec_alloc_context3(codec);
  97. if (!ctx) {
  98. av_log(NULL, AV_LOG_ERROR, "Can't allocate decoder context\n");
  99. return AVERROR(ENOMEM);
  100. }
  101. result = avcodec_parameters_to_context(ctx, origin_par);
  102. if (result) {
  103. av_log(NULL, AV_LOG_ERROR, "Can't copy decoder context\n");
  104. return result;
  105. }
  106. ctx->draw_horiz_band = draw_horiz_band;
  107. ctx->thread_count = 1;
  108. result = avcodec_open2(ctx, codec, NULL);
  109. if (result < 0) {
  110. av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n");
  111. return result;
  112. }
  113. fr = av_frame_alloc();
  114. if (!fr) {
  115. av_log(NULL, AV_LOG_ERROR, "Can't allocate frame\n");
  116. return AVERROR(ENOMEM);
  117. }
  118. if (strcmp(codec->name, "flv") && strcmp(codec->name, "mpeg4") && strcmp(codec->name, "huffyuv")) {
  119. av_log(NULL, AV_LOG_ERROR, "Wrong codec\n");
  120. return -1;
  121. }
  122. byte_buffer_size = av_image_get_buffer_size(ctx->pix_fmt, ctx->width, ctx->height, 32);
  123. byte_buffer = av_malloc(byte_buffer_size);
  124. if (!byte_buffer) {
  125. av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n");
  126. return AVERROR(ENOMEM);
  127. }
  128. slice_byte_buffer = av_malloc(byte_buffer_size);
  129. if (!slice_byte_buffer) {
  130. av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n");
  131. return AVERROR(ENOMEM);
  132. }
  133. memset(slice_byte_buffer, 0, byte_buffer_size);
  134. slice_byte_buffer_size = byte_buffer_size;
  135. av_init_packet(&pkt);
  136. do {
  137. if (!end_of_stream) {
  138. if (av_read_frame(fmt_ctx, &pkt) < 0) {
  139. end_of_stream = 1;
  140. }
  141. }
  142. if (end_of_stream) {
  143. pkt.data = NULL;
  144. pkt.size = 0;
  145. }
  146. if (pkt.stream_index == video_stream || end_of_stream) {
  147. got_frame = 0;
  148. result = avcodec_decode_video2(ctx, fr, &got_frame, &pkt);
  149. if (result < 0) {
  150. av_log(NULL, AV_LOG_ERROR, "Error decoding frame\n");
  151. return result;
  152. }
  153. if (got_frame) {
  154. number_of_written_bytes = av_image_copy_to_buffer(byte_buffer, byte_buffer_size,
  155. (const uint8_t* const *)fr->data, (const int*) fr->linesize,
  156. ctx->pix_fmt, ctx->width, ctx->height, 1);
  157. if (number_of_written_bytes < 0) {
  158. av_log(NULL, AV_LOG_ERROR, "Can't copy image to buffer\n");
  159. return number_of_written_bytes;
  160. }
  161. if (draw_horiz_band_called == 0) {
  162. av_log(NULL, AV_LOG_ERROR, "draw_horiz_band haven't been called!\n");
  163. return -1;
  164. }
  165. if (av_adler32_update(0, (const uint8_t*)byte_buffer, number_of_written_bytes) !=
  166. av_adler32_update(0, (const uint8_t*)slice_byte_buffer, number_of_written_bytes)) {
  167. av_log(NULL, AV_LOG_ERROR, "Decoded frames with and without draw_horiz_band are not the same!\n");
  168. return -1;
  169. }
  170. }
  171. av_packet_unref(&pkt);
  172. av_init_packet(&pkt);
  173. }
  174. } while (!end_of_stream || got_frame);
  175. av_packet_unref(&pkt);
  176. av_frame_free(&fr);
  177. avcodec_close(ctx);
  178. avformat_close_input(&fmt_ctx);
  179. avcodec_free_context(&ctx);
  180. av_freep(&byte_buffer);
  181. av_freep(&slice_byte_buffer);
  182. return 0;
  183. }
  184. int main(int argc, char **argv)
  185. {
  186. if (argc < 2)
  187. {
  188. av_log(NULL, AV_LOG_ERROR, "Incorrect input: expected %s <name of a video file>\nNote that test works only for huffyuv, flv and mpeg4 decoders\n", argv[0]);
  189. return 1;
  190. }
  191. av_register_all();
  192. if (video_decode(argv[1]) != 0)
  193. return 1;
  194. return 0;
  195. }