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.

235 lines
7.8KB

  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 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. ctx = avcodec_alloc_context3(codec);
  95. if (!ctx) {
  96. av_log(NULL, AV_LOG_ERROR, "Can't allocate decoder context\n");
  97. return AVERROR(ENOMEM);
  98. }
  99. result = avcodec_parameters_to_context(ctx, origin_par);
  100. if (result) {
  101. av_log(NULL, AV_LOG_ERROR, "Can't copy decoder context\n");
  102. return result;
  103. }
  104. ctx->draw_horiz_band = draw_horiz_band;
  105. ctx->thread_count = 1;
  106. result = avcodec_open2(ctx, codec, NULL);
  107. if (result < 0) {
  108. av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n");
  109. return result;
  110. }
  111. fr = av_frame_alloc();
  112. if (!fr) {
  113. av_log(NULL, AV_LOG_ERROR, "Can't allocate frame\n");
  114. return AVERROR(ENOMEM);
  115. }
  116. pkt = av_packet_alloc();
  117. if (!pkt) {
  118. av_log(NULL, AV_LOG_ERROR, "Cannot allocate packet\n");
  119. return AVERROR(ENOMEM);
  120. }
  121. if (strcmp(codec->name, "flv") && strcmp(codec->name, "mpeg4") && strcmp(codec->name, "huffyuv")) {
  122. av_log(NULL, AV_LOG_ERROR, "Wrong codec\n");
  123. return -1;
  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. slice_byte_buffer_size = byte_buffer_size;
  138. result = 0;
  139. while (result >= 0) {
  140. result = av_read_frame(fmt_ctx, pkt);
  141. if (result >= 0 && pkt->stream_index != video_stream) {
  142. av_packet_unref(pkt);
  143. continue;
  144. }
  145. // pkt will be empty on read error/EOF
  146. result = avcodec_send_packet(ctx, pkt);
  147. av_packet_unref(pkt);
  148. if (result < 0) {
  149. av_log(NULL, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
  150. return result;
  151. }
  152. while (result >= 0) {
  153. result = avcodec_receive_frame(ctx, fr);
  154. if (result == AVERROR_EOF)
  155. goto finish;
  156. else if (result == AVERROR(EAGAIN)) {
  157. result = 0;
  158. break;
  159. } else if (result < 0) {
  160. av_log(NULL, AV_LOG_ERROR, "Error decoding frame\n");
  161. return result;
  162. }
  163. number_of_written_bytes = av_image_copy_to_buffer(byte_buffer, byte_buffer_size,
  164. (const uint8_t* const *)fr->data, (const int*) fr->linesize,
  165. ctx->pix_fmt, ctx->width, ctx->height, 1);
  166. if (number_of_written_bytes < 0) {
  167. av_log(NULL, AV_LOG_ERROR, "Can't copy image to buffer\n");
  168. return number_of_written_bytes;
  169. }
  170. if (draw_horiz_band_called == 0) {
  171. av_log(NULL, AV_LOG_ERROR, "draw_horiz_band haven't been called!\n");
  172. return -1;
  173. }
  174. if (av_adler32_update(0, (const uint8_t*)byte_buffer, number_of_written_bytes) !=
  175. av_adler32_update(0, (const uint8_t*)slice_byte_buffer, number_of_written_bytes)) {
  176. av_log(NULL, AV_LOG_ERROR, "Decoded frames with and without draw_horiz_band are not the same!\n");
  177. return -1;
  178. }
  179. av_frame_unref(fr);
  180. }
  181. }
  182. finish:
  183. av_packet_free(&pkt);
  184. av_frame_free(&fr);
  185. avformat_close_input(&fmt_ctx);
  186. avcodec_free_context(&ctx);
  187. av_freep(&byte_buffer);
  188. av_freep(&slice_byte_buffer);
  189. return 0;
  190. }
  191. int main(int argc, char **argv)
  192. {
  193. if (argc < 2)
  194. {
  195. 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]);
  196. return 1;
  197. }
  198. if (video_decode(argv[1]) != 0)
  199. return 1;
  200. return 0;
  201. }