/src/mpv/video/image_loader.c
Line | Count | Source |
1 | | #include <libavcodec/avcodec.h> |
2 | | |
3 | | #include "common/common.h" |
4 | | #include "mp_image.h" |
5 | | #include "player/screenshot.h" |
6 | | |
7 | | #include "image_loader.h" |
8 | | |
9 | | struct mp_image *load_image_png_buf(void *buffer, size_t buffer_size, int imgfmt) |
10 | 0 | { |
11 | 0 | const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_PNG); |
12 | 0 | if (!codec) |
13 | 0 | return NULL; |
14 | | |
15 | 0 | AVCodecContext *avctx = avcodec_alloc_context3(codec); |
16 | 0 | if (!avctx) |
17 | 0 | return NULL; |
18 | | |
19 | 0 | if (avcodec_open2(avctx, codec, NULL) < 0) { |
20 | 0 | avcodec_free_context(&avctx); |
21 | 0 | return NULL; |
22 | 0 | } |
23 | | |
24 | 0 | AVPacket *pkt = av_packet_alloc(); |
25 | 0 | if (pkt) { |
26 | 0 | if (av_new_packet(pkt, buffer_size) >= 0) |
27 | 0 | memcpy(pkt->data, buffer, buffer_size); |
28 | 0 | } |
29 | | |
30 | | // (There is only 1 outcome: either it takes it and decodes it, or not.) |
31 | 0 | avcodec_send_packet(avctx, pkt); |
32 | 0 | avcodec_send_packet(avctx, NULL); |
33 | |
|
34 | 0 | av_packet_free(&pkt); |
35 | |
|
36 | 0 | struct mp_image *res = NULL; |
37 | 0 | AVFrame *frame = av_frame_alloc(); |
38 | 0 | if (frame && avcodec_receive_frame(avctx, frame) >= 0) { |
39 | 0 | struct mp_image *r = mp_image_from_av_frame(frame); |
40 | 0 | if (r) |
41 | 0 | res = convert_image(r, imgfmt, NULL, mp_null_log); |
42 | 0 | talloc_free(r); |
43 | 0 | } |
44 | 0 | av_frame_free(&frame); |
45 | |
|
46 | 0 | avcodec_free_context(&avctx); |
47 | 0 | return res; |
48 | 0 | } |