api-band-test.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "libavutil/mem.h"
  27. #include "libavcodec/avcodec.h"
  28. #include "libavformat/avformat.h"
  29. #include "libavutil/imgutils.h"
  30. uint8_t *slice_byte_buffer;
  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. const 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 byte_buffer_size;
  71. int result;
  72. draw_horiz_band_called = 0;
  73. result = avformat_open_input(&fmt_ctx, input_filename, NULL, NULL);
  74. if (result < 0) {
  75. av_log(NULL, AV_LOG_ERROR, "Can't open file\n");
  76. return result;
  77. }
  78. result = avformat_find_stream_info(fmt_ctx, NULL);
  79. if (result < 0) {
  80. av_log(NULL, AV_LOG_ERROR, "Can't get stream info\n");
  81. return result;
  82. }
  83. video_stream = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  84. if (video_stream < 0) {
  85. av_log(NULL, AV_LOG_ERROR, "Can't find video stream in input file\n");
  86. return -1;
  87. }
  88. origin_par = fmt_ctx->streams[video_stream]->codecpar;
  89. codec = avcodec_find_decoder(origin_par->codec_id);
  90. if (!codec) {
  91. av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");
  92. return -1;
  93. }
  94. if (!(codec->capabilities & AV_CODEC_CAP_DRAW_HORIZ_BAND)) {
  95. av_log(NULL, AV_LOG_ERROR, "Codec does not support draw_horiz_band\n");
  96. return -1;
  97. }
  98. ctx = avcodec_alloc_context3(codec);
  99. if (!ctx) {
  100. av_log(NULL, AV_LOG_ERROR, "Can't allocate decoder context\n");
  101. return AVERROR(ENOMEM);
  102. }
  103. result = avcodec_parameters_to_context(ctx, origin_par);
  104. if (result) {
  105. av_log(NULL, AV_LOG_ERROR, "Can't copy decoder context\n");
  106. return result;
  107. }
  108. ctx->draw_horiz_band = draw_horiz_band;
  109. ctx->thread_count = 1;
  110. result = avcodec_open2(ctx, codec, NULL);
  111. if (result < 0) {
  112. av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n");
  113. return result;
  114. }
  115. fr = av_frame_alloc();
  116. if (!fr) {
  117. av_log(NULL, AV_LOG_ERROR, "Can't allocate frame\n");
  118. return AVERROR(ENOMEM);
  119. }
  120. pkt = av_packet_alloc();
  121. if (!pkt) {
  122. av_log(NULL, AV_LOG_ERROR, "Cannot allocate packet\n");
  123. return AVERROR(ENOMEM);
  124. }
  125. byte_buffer_size = av_image_get_buffer_size(ctx->pix_fmt, ctx->width, ctx->height, 32);
  126. byte_buffer = av_malloc(byte_buffer_size);
  127. if (!byte_buffer) {
  128. av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n");
  129. return AVERROR(ENOMEM);
  130. }
  131. slice_byte_buffer = av_malloc(byte_buffer_size);
  132. if (!slice_byte_buffer) {
  133. av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n");
  134. return AVERROR(ENOMEM);
  135. }
  136. memset(slice_byte_buffer, 0, byte_buffer_size);
  137. result = 0;
  138. while (result >= 0) {
  139. result = av_read_frame(fmt_ctx, pkt);
  140. if (result >= 0 && pkt->stream_index != video_stream) {
  141. av_packet_unref(pkt);
  142. continue;
  143. }
  144. // pkt will be empty on read error/EOF
  145. result = avcodec_send_packet(ctx, pkt);
  146. av_packet_unref(pkt);
  147. if (result < 0) {
  148. av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
  149. return result;
  150. }
  151. while (result >= 0) {
  152. result = avcodec_receive_frame(ctx, fr);
  153. if (result == AVERROR_EOF)
  154. goto finish;
  155. else if (result == AVERROR(EAGAIN)) {
  156. result = 0;
  157. break;
  158. } else if (result < 0) {
  159. av_log(NULL, AV_LOG_ERROR, "Error decoding frame\n");
  160. return result;
  161. }
  162. number_of_written_bytes = av_image_copy_to_buffer(byte_buffer, byte_buffer_size,
  163. (const uint8_t* const *)fr->data, (const int*) fr->linesize,
  164. ctx->pix_fmt, ctx->width, ctx->height, 1);
  165. if (number_of_written_bytes < 0) {
  166. av_log(NULL, AV_LOG_ERROR, "Can't copy image to buffer\n");
  167. return number_of_written_bytes;
  168. }
  169. if (draw_horiz_band_called == 0) {
  170. av_log(NULL, AV_LOG_ERROR, "draw_horiz_band haven't been called!\n");
  171. return -1;
  172. }
  173. if (av_adler32_update(0, (const uint8_t*)byte_buffer, number_of_written_bytes) !=
  174. av_adler32_update(0, (const uint8_t*)slice_byte_buffer, number_of_written_bytes)) {
  175. av_log(NULL, AV_LOG_ERROR, "Decoded frames with and without draw_horiz_band are not the same!\n");
  176. return -1;
  177. }
  178. av_frame_unref(fr);
  179. }
  180. }
  181. finish:
  182. av_packet_free(&pkt);
  183. av_frame_free(&fr);
  184. avformat_close_input(&fmt_ctx);
  185. avcodec_free_context(&ctx);
  186. av_freep(&byte_buffer);
  187. av_freep(&slice_byte_buffer);
  188. return 0;
  189. }
  190. int main(int argc, char **argv)
  191. {
  192. if (argc < 2)
  193. {
  194. av_log(NULL, AV_LOG_ERROR, "Incorrect input: expected %s <name of a video file>\n", argv[0]);
  195. return 1;
  196. }
  197. if (video_decode(argv[1]) != 0)
  198. return 1;
  199. return 0;
  200. }