FFmpeg源码剖析-解码:av_read_frame()

本文详细介绍了FFmpeg中的av_read_frame()函数,该函数用于从输入流中读取下一帧数据。文章深入探讨了函数的工作原理、参数含义及返回值解释,并通过伪码与代码分析展示了其内部实现细节。
av_read_frame()函数位于libavformat/utils.c

1. 函数概述

读取输入流的一帧数据;


2. 函数调用图


3. 伪码分析





4. 代码分析

av_read_frame()它的声明(libavformat/avformat.h)/**
 * Return the next frame of a stream.
 * This function returns what is stored in the file, and does not validate
 * that what is there are valid frames for the decoder. It will split what is
 * stored in the file into frames and return one for each call. It will not
 * omit invalid data between valid frames so as to give the decoder the maximum
 * information possible for decoding.
 *
 * If pkt->buf is NULL, then the packet is valid until the next
 * av_read_frame() or until avformat_close_input(). Otherwise the packet
 * is valid indefinitely. In both cases the packet must be freed with
 * av_packet_unref when it is no longer needed. For video, the packet contains
 * exactly one frame. For audio, it contains an integer number of frames if each
 * frame has a known fixed size (e.g. PCM or ADPCM data). If the audio frames
 * have a variable size (e.g. MPEG audio), then it contains one frame.
 *
 * pkt->pts, pkt->dts and pkt->duration are always set to correct
 * values in AVStream.time_base units (and guessed if the format cannot
 * provide them). pkt->pts can be AV_NOPTS_VALUE if the video format
 * has B-frames, so it is better to rely on pkt->dts if you do not
 * decompress the payload.
 *
 * @return 0 if OK, < 0 on error or end of file
 */
int av_read_frame(AVFormatContext *s, AVPacket *pkt);

函数功能: 返回输入流的下一帧。
    因为它是为解码器提供尽可能多的信息,所以它返回的是存储在文件中的下一帧,
    而不会验证这一帧对解码器是否有效。
    
参数解析:
 . pkt->buf, 
   如果它的值为NULL,那这个Packet数据有效,直到下一个av_read_frame()或 avformat_close_input()。
   否则,这个packet数据不确定是否有效。
   在这两种情况下,当数据不再有效时,需要用av_packet_unref来释放。
   对于视频来说,这个packet包含一个完整的视频帧;
   对于音频来说,如果音频帧的大小是固定的(如PCM或ADPCM数据),则包含整数帧;
                 如果音频帧的大小是可变的(如MPEG音频),则包含一帧。
 . pkt->pts, pkt->dts和pkt->duration是以AVStream.time_base为单位表示的。
   pkt->pts,值可以是AV_NOPTS_VALUE,如果视频中包含B-frames。
   所以当不进行数据解码时,pkt->dts是更可靠的。
              
返回值:
    返回值为 0 ,则正确;
    返回值小于0,则表示出错或到了文件尾;


它的定义(libavformat/utils.c):
#define AVFMT_FLAG_GENPTS       0x0001 ///< Generate missing pts even if it requires parsing future frames.

int av_read_frame(AVFormatContext *s, AVPacket *pkt)
{
    const int genpts = s->flags & AVFMT_FLAG_GENPTS;
    int eof = 0;
    int ret;
    AVStream *st;

    if (!genpts) {
        /* 如果包的链表中有数据,则直接从链表中取一个packet; 
         * 如果没有,则要读文件获得一个packet.
         */
        ret = s->internal->packet_buffer
              ? read_from_packet_buffer(&s->internal->packet_buffer,
                                        &s->internal->packet_buffer_end, pkt)
              : read_frame_internal(s, pkt);
        if (ret < 0)
            return ret;
        goto return_packet;
    }

    for (;;) {
        AVPacketList *pktl = s->internal->packet_buffer;

        if (pktl) {
            AVPacket *next_pkt = &pktl->pkt;

            if (next_pkt->dts != AV_NOPTS_VALUE) {
                int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;

                // last dts seen for this stream. if any of packets following
                // current one had no dts, we will set this to AV_NOPTS_VALUE.
                int64_t last_dts = next_pkt->dts;
                while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
                    if (pktl->pkt.stream_index == next_pkt->stream_index &&
                        (av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2LL << (wrap_bits - 1)) < 0)) {
                        if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2LL << (wrap_bits - 1))) {
                            // not B-frame
                            next_pkt->pts = pktl->pkt.dts;
                        }
                        if (last_dts != AV_NOPTS_VALUE) {
                            // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
                            last_dts = pktl->pkt.dts;
                        }
                    }
                    pktl = pktl->next;
                }
                if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
                    // Fixing the last reference frame had none pts issue (For MXF etc).
                    // We only do this when
                    // 1. eof.
                    // 2. we are not able to resolve a pts value for current packet.
                    // 3. the packets for this stream at the end of the files had valid dts.
                    next_pkt->pts = last_dts + next_pkt->duration;
                }
                pktl = s->internal->packet_buffer;
            }

            /* read packet from packet buffer, if there is data */
            st = s->streams[next_pkt->stream_index];
            if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
                  next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
                ret = read_from_packet_buffer(&s->internal->packet_buffer,
                                               &s->internal->packet_buffer_end, pkt);
                goto return_packet;
            }
        }

        ret = read_frame_internal(s, pkt);
        if (ret < 0) {
            if (pktl && ret != AVERROR(EAGAIN)) {
                eof = 1;
                continue;
            } else
                return ret;
        }

        ret = add_to_pktbuf(&s->internal->packet_buffer, pkt,
                            &s->internal->packet_buffer_end, 1);
        av_packet_unref(pkt);
        if (ret < 0)
            return ret;
    }

return_packet:
    st = s->streams[pkt->stream_index];
    if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
        ff_reduce_index(s, st->index);
        av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
    }

    if (is_relative(pkt->dts))
        pkt->dts -= RELATIVE_TS_BASE;
    if (is_relative(pkt->pts))
        pkt->pts -= RELATIVE_TS_BASE;

    return ret;
}

4.1 read_from_packet_buffer

static int read_from_packet_buffer(AVPacketList **pkt_buffer,
                                   AVPacketList **pkt_buffer_end,
                                   AVPacket      *pkt)
{
    AVPacketList *pktl;
    av_assert0(*pkt_buffer);


    pktl        = *pkt_buffer;
    *pkt        = pktl->pkt;
    *pkt_buffer = pktl->next;
    if (!pktl->next)
        *pkt_buffer_end = NULL;
    av_freep(&pktl);
    return 0;
}

这个函数很简单,
就是从packet链表的头起取一个packet,然后将链表pkt_buffer指针向下移动一个。

4.2 read_frame_internal()

read_frame_internal()位于 libavformat/utils.c
static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
{
    int ret = 0, i, got_packet = 0;
    AVDictionary *metadata = NULL;


    /* AVPacket结构体的初始化 */
    av_init_packet(pkt);


    while (!got_packet && !s->internal->parse_queue) {
        AVStream *st;
        AVPacket cur_pkt;


        /* read next packet */
        ret = ff_read_packet(s, &cur_pkt);
        if (ret < 0) { ... }
        ret = 0;
        st  = s->streams[cur_pkt.stream_index]; //从全局变量中获得当前包所在流的数据信息


        /* update context if required */
        if (st->internal->need_context_update) {
          ...
        }


        if (!st->need_parsing || !st->parser) {
            /* no parsing needed: we just output the packet as is */
            *pkt = cur_pkt;
            compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
            if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
                (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
                ff_reduce_index(s, st->index);
                av_add_index_entry(st, pkt->pos, pkt->dts,
                                   0, 0, AVINDEX_KEYFRAME);
            }
            got_packet = 1;
        } else if (st->discard < AVDISCARD_ALL) {
            /* 解析当前的packet,并将所有解析出的数据填充到结构体中 */
            if ((ret = parse_packet(s, &cur_pkt, cur_pkt.stream_index)) < 0)
                return ret;
            st->codecpar->sample_rate = st->internal->avctx->sample_rate;
            st->codecpar->bit_rate = st->internal->avctx->bit_rate;
            st->codecpar->channels = st->internal->avctx->channels;
            st->codecpar->channel_layout = st->internal->avctx->channel_layout;
            st->codecpar->codec_id = st->internal->avctx->codec_id;
        } else {
            /* free packet */
            av_packet_unref(&cur_pkt);
        }
        if (pkt->flags & AV_PKT_FLAG_KEY)
            st->skip_to_keyframe = 0;
        if (st->skip_to_keyframe) {
            av_packet_unref(&cur_pkt);
            if (got_packet) {
                *pkt = cur_pkt;
            }
            got_packet = 0;
        }
    }


    /* 再从上面已读取新包到包链表中取出一个包 */
    if (!got_packet && s->internal->parse_queue)
        ret = read_from_packet_buffer(&s->internal->parse_queue, &s->internal->parse_queue_end, pkt);


    if (ret >= 0) {
        AVStream *st = s->streams[pkt->stream_index];
        int discard_padding = 0;
        if (st->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
            int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0);
            int64_t sample = ts_to_samples(st, pts);
            int duration = ts_to_samples(st, pkt->duration);
            int64_t end_sample = sample + duration;
            if (duration > 0 && end_sample >= st->first_discard_sample &&
                sample < st->last_discard_sample)
                discard_padding = FFMIN(end_sample - st->first_discard_sample, duration);
        }
        if (st->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
            st->skip_samples = st->start_skip_samples;
        if (st->skip_samples || discard_padding) {
            uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
            if (p) {
                AV_WL32(p, st->skip_samples);
                AV_WL32(p + 4, discard_padding);
                av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %d / discard %d\n", st->skip_samples, discard_padding);
            }
            st->skip_samples = 0;
        }


        if (st->inject_global_side_data) {
            for (i = 0; i < st->nb_side_data; i++) {
                AVPacketSideData *src_sd = &st->side_data[i];
                uint8_t *dst_data;


                if (av_packet_get_side_data(pkt, src_sd->type, NULL))
                    continue;


                dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
                if (!dst_data) {
                    av_log(s, AV_LOG_WARNING, "Could not inject global side data\n");
                    continue;
                }


                memcpy(dst_data, src_sd->data, src_sd->size);
            }
            st->inject_global_side_data = 0;
        }


        if (!(s->flags & AVFMT_FLAG_KEEP_SIDE_DATA))
            av_packet_merge_side_data(pkt);
    }


    av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
    if (metadata) {
        s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
        av_dict_copy(&s->metadata, metadata, 0);
        av_dict_free(&metadata);
        av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN);
    }


#if FF_API_LAVF_AVCTX
    update_stream_avctx(s);
#endif


    if (s->debug & FF_FDEBUG_TS)
        av_log(s, AV_LOG_DEBUG,
               "read_frame_internal stream=%d, pts=%s, dts=%s, "
               "size=%d, duration=%"PRId64", flags=%d\n",
               pkt->stream_index,
               av_ts2str(pkt->pts),
               av_ts2str(pkt->dts),
               pkt->size, pkt->duration, pkt->flags);


    return ret;
}


4.3 parse_packet()

libavformat/utils.c
/**
 * Parse a packet, add all split parts to parse_queue.
 *
 * @param pkt Packet to parse, NULL when flushing the parser at end of stream.
 */
static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index)
{
    AVPacket out_pkt = { 0 }, flush_pkt = { 0 };
    AVStream *st = s->streams[stream_index];
    uint8_t *data = pkt ? pkt->data : NULL;
    int size      = pkt ? pkt->size : 0;
    int ret = 0, got_output = 0;


    if (!pkt) {
        av_init_packet(&flush_pkt);
        pkt        = &flush_pkt;
        got_output = 1;
    } else if (!size && st->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) {
        // preserve 0-size sync packets
        compute_pkt_fields(s, st, st->parser, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
    }


    while (size > 0 || (pkt == &flush_pkt && got_output)) {
        int len;
        int64_t next_pts = pkt->pts;
        int64_t next_dts = pkt->dts;


        av_init_packet(&out_pkt);
        /* 调用视频或音频解析器进行对应的解析 */
        len = av_parser_parse2(st->parser, st->internal->avctx,
                               &out_pkt.data, &out_pkt.size, data, size,
                               pkt->pts, pkt->dts, pkt->pos);


        pkt->pts = pkt->dts = AV_NOPTS_VALUE;
        pkt->pos = -1;
        /* increment read pointer */
        data += len;
        size -= len;


        got_output = !!out_pkt.size;


        if (!out_pkt.size)
            continue;


        if (pkt->side_data) {
            out_pkt.side_data       = pkt->side_data;
            out_pkt.side_data_elems = pkt->side_data_elems;
            pkt->side_data          = NULL;
            pkt->side_data_elems    = 0;
        }


        /* set the duration */
        out_pkt.duration = (st->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
        if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
            if (st->internal->avctx->sample_rate > 0) {
                out_pkt.duration =
                    av_rescale_q_rnd(st->parser->duration,
                                     (AVRational) { 1, st->internal->avctx->sample_rate },
                                     st->time_base,
                                     AV_ROUND_DOWN);
            }
        }


        out_pkt.stream_index = st->index;
        out_pkt.pts          = st->parser->pts;
        out_pkt.dts          = st->parser->dts;
        out_pkt.pos          = st->parser->pos;


        if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW)
            out_pkt.pos = st->parser->frame_offset;


        if (st->parser->key_frame == 1 ||
            (st->parser->key_frame == -1 &&
             st->parser->pict_type == AV_PICTURE_TYPE_I))
            out_pkt.flags |= AV_PKT_FLAG_KEY;


        if (st->parser->key_frame == -1 && st->parser->pict_type ==AV_PICTURE_TYPE_NONE && (pkt->flags&AV_PKT_FLAG_KEY))
            out_pkt.flags |= AV_PKT_FLAG_KEY;


        /* 计算当前包的pts,dts,duration三个值 */
        compute_pkt_fields(s, st, st->parser, &out_pkt, next_dts, next_pts);


        /* 将当前包添加到包链表中 */
        ret = add_to_pktbuf(&s->internal->parse_queue, &out_pkt,
                            &s->internal->parse_queue_end, 1);
        av_packet_unref(&out_pkt);
        if (ret < 0)
            goto fail;
    }


    /* end of the stream => close and free the parser */
    if (pkt == &flush_pkt) {
        av_parser_close(st->parser);
        st->parser = NULL;
    }


fail:
    av_packet_unref(pkt);
    return ret;
}


4.4 av_parser_parse2()

libavformat/avcodec.h
/**
 * Parse a packet.
 *
 * @param s             parser context.
 * @param avctx         codec context.
 * @param poutbuf       set to pointer to parsed buffer or NULL if not yet finished.
 * @param poutbuf_size  set to size of parsed buffer or zero if not yet finished.
 * @param buf           input buffer.
 * @param buf_size      buffer size in bytes without the padding. I.e. the full buffer
                        size is assumed to be buf_size + AV_INPUT_BUFFER_PADDING_SIZE.
                        To signal EOF, this should be 0 (so that the last frame
                        can be output).
 * @param pts           input presentation timestamp.
 * @param dts           input decoding timestamp.
 * @param pos           input byte position in stream.
 * @return the number of bytes of the input bitstream used.
 *
 * Example:
 * @code
 *   while(in_len){
 *       len = av_parser_parse2(myparser, AVCodecContext, &data, &size,
 *                                        in_data, in_len,
 *                                        pts, dts, pos);
 *       in_data += len;
 *       in_len  -= len;
 *
 *       if(size)
 *          decode_frame(data, size);
 *   }
 * @endcode
 */
int av_parser_parse2(AVCodecParserContext *s,
                     AVCodecContext *avctx,
                     uint8_t **poutbuf, int *poutbuf_size,
                     const uint8_t *buf, int buf_size,
                     int64_t pts, int64_t dts,
                     int64_t pos);
解析一个packet。


libavcodec/parser.c
int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx,
                     uint8_t **poutbuf, int *poutbuf_size,
                     const uint8_t *buf, int buf_size,
                     int64_t pts, int64_t dts, int64_t pos)
{
    int index, i;
    uint8_t dummy_buf[AV_INPUT_BUFFER_PADDING_SIZE];


    av_assert1(avctx->codec_id != AV_CODEC_ID_NONE);


    /* Parsers only work for the specified codec ids. */
    av_assert1(avctx->codec_id == s->parser->codec_ids[0] ||
               avctx->codec_id == s->parser->codec_ids[1] ||
               avctx->codec_id == s->parser->codec_ids[2] ||
               avctx->codec_id == s->parser->codec_ids[3] ||
               avctx->codec_id == s->parser->codec_ids[4]);


    if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
        s->next_frame_offset =
        s->cur_offset        = pos;
        s->flags            |= PARSER_FLAG_FETCHED_OFFSET;
    }


    if (buf_size == 0) {
        /* padding is always necessary even if EOF, so we add it here */
        memset(dummy_buf, 0, sizeof(dummy_buf));
        buf = dummy_buf;
    } else if (s->cur_offset + buf_size != s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */
        /* add a new packet descriptor */
        i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
        s->cur_frame_start_index = i;
        s->cur_frame_offset[i]   = s->cur_offset;
        s->cur_frame_end[i]      = s->cur_offset + buf_size;
        s->cur_frame_pts[i]      = pts;
        s->cur_frame_dts[i]      = dts;
        s->cur_frame_pos[i]      = pos;
    }


    if (s->fetch_timestamp) {
        s->fetch_timestamp = 0;
        s->last_pts        = s->pts;
        s->last_dts        = s->dts;
        s->last_pos        = s->pos;
        ff_fetch_timestamp(s, 0, 0, 0);
    }
    /* WARNING: the returned index can be negative */
    /* 此处函数指针将指向当前包所对应流的解析器,
     * 如H.264的视频流,则调用libavcodec/h264_parser.c:h264_parse()函数,
     * 它将分解析H.264的NAL单元, 从而获得这个包的完整信息 
     */
    index = s->parser->parser_parse(s, avctx, (const uint8_t **) poutbuf,
                                    poutbuf_size, buf, buf_size);
    av_assert0(index > -0x20000000); // The API does not allow returning AVERROR codes
#define FILL(name) if(s->name > 0 && avctx->name <= 0) avctx->name = s->name
    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
        FILL(field_order);
    }


    /* update the file pointer */
    if (*poutbuf_size) {
        /* fill the data for the current frame */
        s->frame_offset = s->next_frame_offset;


        /* offset of the next frame */
        s->next_frame_offset = s->cur_offset + index;
        s->fetch_timestamp   = 1;
    }
    if (index < 0)
        index = 0;
    s->cur_offset += index;
    return index;
}




/** * @file lv_ffmpeg.c * */ /********************* * INCLUDES *********************/ #include "lv_ffmpeg_private.h" #if LV_USE_FFMPEG != 0 #include "../../draw/lv_image_decoder_private.h" #include "../../draw/lv_draw_buf_private.h" #include "../../core/lv_obj_class_private.h" #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/imgutils.h> #include <libavutil/samplefmt.h> #include <libavutil/timestamp.h> #include <libswscale/swscale.h> /********************* * DEFINES *********************/ #define DECODER_NAME "FFMPEG" #if LV_COLOR_DEPTH == 8 #define AV_PIX_FMT_TRUE_COLOR AV_PIX_FMT_RGB8 #elif LV_COLOR_DEPTH == 16 #define AV_PIX_FMT_TRUE_COLOR AV_PIX_FMT_RGB565LE #elif LV_COLOR_DEPTH == 32 #define AV_PIX_FMT_TRUE_COLOR AV_PIX_FMT_BGR0 #else #error Unsupported LV_COLOR_DEPTH #endif #define MY_CLASS (&lv_ffmpeg_player_class) #define FRAME_DEF_REFR_PERIOD 33 /*[ms]*/ #define DECODER_BUFFER_SIZE (8 * 1024) /********************** * TYPEDEFS **********************/ struct ffmpeg_context_s { AVIOContext * io_ctx; lv_fs_file_t lv_file; AVFormatContext * fmt_ctx; AVCodecContext * video_dec_ctx; AVStream * video_stream; uint8_t * video_src_data[4]; uint8_t * video_dst_data[4]; struct SwsContext * sws_ctx; AVFrame * frame; AVPacket * pkt; int video_stream_idx; int video_src_linesize[4]; int video_dst_linesize[4]; enum AVPixelFormat video_dst_pix_fmt; bool has_alpha; lv_draw_buf_t draw_buf; lv_draw_buf_handlers_t draw_buf_handlers; }; #pragma pack(1) struct _lv_image_pixel_color_s { lv_color_t c; uint8_t alpha; }; #pragma pack() /********************** * STATIC PROTOTYPES **********************/ static lv_result_t decoder_info(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * src, lv_image_header_t * header); static lv_result_t decoder_open(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * dsc); static void decoder_close(lv_image_decoder_t * dec, lv_image_decoder_dsc_t * dsc); static int ffmpeg_lvfs_read(void * ptr, uint8_t * buf, int buf_size); static int64_t ffmpeg_lvfs_seek(void * ptr, int64_t pos, int whence); static AVIOContext * ffmpeg_open_io_context(lv_fs_file_t * file); static struct ffmpeg_context_s * ffmpeg_open_file(const char * path, bool is_lv_fs_path); static void ffmpeg_close(struct ffmpeg_context_s * ffmpeg_ctx); static void ffmpeg_close_src_ctx(struct ffmpeg_context_s * ffmpeg_ctx); static void ffmpeg_close_dst_ctx(struct ffmpeg_context_s * ffmpeg_ctx); static int ffmpeg_image_allocate(struct ffmpeg_context_s * ffmpeg_ctx); static int ffmpeg_get_image_header(lv_image_decoder_dsc_t * dsc, lv_image_header_t * header); static int ffmpeg_get_frame_refr_period(struct ffmpeg_context_s * ffmpeg_ctx); static uint8_t * ffmpeg_get_image_data(struct ffmpeg_context_s * ffmpeg_ctx); static int ffmpeg_update_next_frame(struct ffmpeg_context_s * ffmpeg_ctx); static int ffmpeg_output_video_frame(struct ffmpeg_context_s * ffmpeg_ctx); static bool ffmpeg_pix_fmt_has_alpha(enum AVPixelFormat pix_fmt); static bool ffmpeg_pix_fmt_is_yuv(enum AVPixelFormat pix_fmt); static void lv_ffmpeg_player_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj); static void lv_ffmpeg_player_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj); /********************** * STATIC VARIABLES **********************/ const lv_obj_class_t lv_ffmpeg_player_class = { .constructor_cb = lv_ffmpeg_player_constructor, .destructor_cb = lv_ffmpeg_player_destructor, .instance_size = sizeof(lv_ffmpeg_player_t), .base_class = &lv_image_class, .name = "lv_ffmpeg_player", }; /********************** * MACROS **********************/ /********************** * GLOBAL FUNCTIONS **********************/ void lv_ffmpeg_init(void) { lv_image_decoder_t * dec = lv_image_decoder_create(); lv_image_decoder_set_info_cb(dec, decoder_info); lv_image_decoder_set_open_cb(dec, decoder_open); lv_image_decoder_set_close_cb(dec, decoder_close); dec->name = DECODER_NAME; #if LV_FFMPEG_DUMP_FORMAT == 0 av_log_set_level(AV_LOG_QUIET); #endif } void lv_ffmpeg_deinit(void) { lv_image_decoder_t * dec = NULL; while((dec = lv_image_decoder_get_next(dec)) != NULL) { if(dec->info_cb == decoder_info) { lv_image_decoder_delete(dec); break; } } } int lv_ffmpeg_get_frame_num(const char * path) { int ret = -1; struct ffmpeg_context_s * ffmpeg_ctx = ffmpeg_open_file(path, LV_FFMPEG_PLAYER_USE_LV_FS); if(ffmpeg_ctx) { ret = ffmpeg_ctx->video_stream->nb_frames; ffmpeg_close(ffmpeg_ctx); } return ret; } lv_obj_t * lv_ffmpeg_player_create(lv_obj_t * parent) { lv_obj_t * obj = lv_obj_class_create_obj(MY_CLASS, parent); lv_obj_class_init_obj(obj); return obj; } lv_result_t lv_ffmpeg_player_set_src(lv_obj_t * obj, const char * path) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_result_t res = LV_RESULT_INVALID; lv_ffmpeg_player_t * player = (lv_ffmpeg_player_t *)obj; if(player->ffmpeg_ctx) { ffmpeg_close(player->ffmpeg_ctx); player->ffmpeg_ctx = NULL; } lv_timer_pause(player->timer); player->ffmpeg_ctx = ffmpeg_open_file(path, LV_FFMPEG_PLAYER_USE_LV_FS); if(!player->ffmpeg_ctx) { goto failed; } if(ffmpeg_image_allocate(player->ffmpeg_ctx) < 0) { LV_LOG_ERROR("ffmpeg image allocate failed"); ffmpeg_close(player->ffmpeg_ctx); player->ffmpeg_ctx = NULL; goto failed; } bool has_alpha = player->ffmpeg_ctx->has_alpha; int width = player->ffmpeg_ctx->video_dec_ctx->width; int height = player->ffmpeg_ctx->video_dec_ctx->height; uint8_t * data = ffmpeg_get_image_data(player->ffmpeg_ctx); lv_color_format_t cf = has_alpha ? LV_COLOR_FORMAT_ARGB8888 : LV_COLOR_FORMAT_NATIVE; uint32_t stride = width * lv_color_format_get_size(cf); uint32_t data_size = stride * height; lv_memzero(data, data_size); player->imgdsc.header.w = width; player->imgdsc.header.h = height; player->imgdsc.data_size = data_size; player->imgdsc.header.cf = cf; player->imgdsc.header.stride = stride; player->imgdsc.data = data; lv_image_set_src(&player->img.obj, &(player->imgdsc)); int period = ffmpeg_get_frame_refr_period(player->ffmpeg_ctx); if(period > 0) { LV_LOG_INFO("frame refresh period = %d ms, rate = %d fps", period, 1000 / period); lv_timer_set_period(player->timer, period); } else { LV_LOG_WARN("unable to get frame refresh period"); } res = LV_RESULT_OK; failed: return res; } void lv_ffmpeg_player_set_cmd(lv_obj_t * obj, lv_ffmpeg_player_cmd_t cmd) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_ffmpeg_player_t * player = (lv_ffmpeg_player_t *)obj; if(!player->ffmpeg_ctx) { LV_LOG_ERROR("ffmpeg_ctx is NULL"); return; } lv_timer_t * timer = player->timer; switch(cmd) { case LV_FFMPEG_PLAYER_CMD_START: av_seek_frame(player->ffmpeg_ctx->fmt_ctx, 0, 0, AVSEEK_FLAG_BACKWARD); lv_timer_resume(timer); LV_LOG_INFO("ffmpeg player start"); break; case LV_FFMPEG_PLAYER_CMD_STOP: av_seek_frame(player->ffmpeg_ctx->fmt_ctx, 0, 0, AVSEEK_FLAG_BACKWARD); lv_timer_pause(timer); LV_LOG_INFO("ffmpeg player stop"); break; case LV_FFMPEG_PLAYER_CMD_PAUSE: lv_timer_pause(timer); LV_LOG_INFO("ffmpeg player pause"); break; case LV_FFMPEG_PLAYER_CMD_RESUME: lv_timer_resume(timer); LV_LOG_INFO("ffmpeg player resume"); break; default: LV_LOG_ERROR("Error cmd: %d", cmd); break; } } void lv_ffmpeg_player_set_auto_restart(lv_obj_t * obj, bool en) { LV_ASSERT_OBJ(obj, MY_CLASS); lv_ffmpeg_player_t * player = (lv_ffmpeg_player_t *)obj; player->auto_restart = en; } /********************** * STATIC FUNCTIONS **********************/ static lv_result_t decoder_info(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * dsc, lv_image_header_t * header) { LV_UNUSED(decoder); /* Get the source type */ lv_image_src_t src_type = dsc->src_type; if(src_type == LV_IMAGE_SRC_FILE) { if(ffmpeg_get_image_header(dsc, header) < 0) { LV_LOG_ERROR("ffmpeg can't get image header"); return LV_RESULT_INVALID; } return LV_RESULT_OK; } /* If didn't succeeded earlier then it's an error */ return LV_RESULT_INVALID; } /** * Decode an image using ffmpeg library * @param decoder pointer to the decoder * @param dsc pointer to the decoder descriptor * @return LV_RESULT_OK: no error; LV_RESULT_INVALID: can't open the image */ static lv_result_t decoder_open(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * dsc) { LV_UNUSED(decoder); if(dsc->src_type == LV_IMAGE_SRC_FILE) { const char * path = dsc->src; struct ffmpeg_context_s * ffmpeg_ctx = ffmpeg_open_file(path, true); if(ffmpeg_ctx == NULL) { return LV_RESULT_INVALID; } if(ffmpeg_image_allocate(ffmpeg_ctx) < 0) { LV_LOG_ERROR("ffmpeg image allocate failed"); ffmpeg_close(ffmpeg_ctx); return LV_RESULT_INVALID; } if(ffmpeg_update_next_frame(ffmpeg_ctx) < 0) { ffmpeg_close(ffmpeg_ctx); LV_LOG_ERROR("ffmpeg update frame failed"); return LV_RESULT_INVALID; } ffmpeg_close_src_ctx(ffmpeg_ctx); uint8_t * img_data = ffmpeg_get_image_data(ffmpeg_ctx); dsc->user_data = ffmpeg_ctx; lv_draw_buf_t * decoded = &ffmpeg_ctx->draw_buf; lv_draw_buf_init( decoded, dsc->header.w, dsc->header.h, dsc->header.cf, dsc->header.stride, img_data, dsc->header.stride * dsc->header.h); lv_draw_buf_set_flag(decoded, LV_IMAGE_FLAGS_MODIFIABLE); /* Empty handlers to avoid decoder asserts */ lv_draw_buf_handlers_init(&ffmpeg_ctx->draw_buf_handlers, NULL, NULL, NULL, NULL, NULL, NULL); decoded->handlers = &ffmpeg_ctx->draw_buf_handlers; if(dsc->args.premultiply && ffmpeg_ctx->has_alpha) { lv_draw_buf_premultiply(decoded); } dsc->decoded = decoded; /* The image is fully decoded. Return with its pointer */ return LV_RESULT_OK; } /* If not returned earlier then it failed */ return LV_RESULT_INVALID; } static void decoder_close(lv_image_decoder_t * decoder, lv_image_decoder_dsc_t * dsc) { LV_UNUSED(decoder); struct ffmpeg_context_s * ffmpeg_ctx = dsc->user_data; ffmpeg_close(ffmpeg_ctx); } static uint8_t * ffmpeg_get_image_data(struct ffmpeg_context_s * ffmpeg_ctx) { uint8_t * img_data = ffmpeg_ctx->video_dst_data[0]; if(img_data == NULL) { LV_LOG_ERROR("ffmpeg video dst data is NULL"); } return img_data; } static bool ffmpeg_pix_fmt_has_alpha(enum AVPixelFormat pix_fmt) { const AVPixFmtDescriptor * desc = av_pix_fmt_desc_get(pix_fmt); if(desc == NULL) { return false; } if(pix_fmt == AV_PIX_FMT_PAL8) { return true; } return desc->flags & AV_PIX_FMT_FLAG_ALPHA; } static bool ffmpeg_pix_fmt_is_yuv(enum AVPixelFormat pix_fmt) { const AVPixFmtDescriptor * desc = av_pix_fmt_desc_get(pix_fmt); if(desc == NULL) { return false; } return !(desc->flags & AV_PIX_FMT_FLAG_RGB) && desc->nb_components >= 2; } static int ffmpeg_output_video_frame(struct ffmpeg_context_s * ffmpeg_ctx) { int ret = -1; int width = ffmpeg_ctx->video_dec_ctx->width; int height = ffmpeg_ctx->video_dec_ctx->height; AVFrame * frame = ffmpeg_ctx->frame; if(frame->width != width || frame->height != height || frame->format != ffmpeg_ctx->video_dec_ctx->pix_fmt) { /* To handle this change, one could call av_image_alloc again and * decode the following frames into another rawvideo file. */ LV_LOG_ERROR("Width, height and pixel format have to be " "constant in a rawvideo file, but the width, height or " "pixel format of the input video changed:\n" "old: width = %d, height = %d, format = %s\n" "new: width = %d, height = %d, format = %s\n", width, height, av_get_pix_fmt_name(ffmpeg_ctx->video_dec_ctx->pix_fmt), frame->width, frame->height, av_get_pix_fmt_name(frame->format)); goto failed; } /* copy decoded frame to destination buffer: * this is required since rawvideo expects non aligned data */ av_image_copy(ffmpeg_ctx->video_src_data, ffmpeg_ctx->video_src_linesize, (const uint8_t **)(frame->data), frame->linesize, ffmpeg_ctx->video_dec_ctx->pix_fmt, width, height); if(ffmpeg_ctx->sws_ctx == NULL) { int swsFlags = SWS_BILINEAR; if(ffmpeg_pix_fmt_is_yuv(ffmpeg_ctx->video_dec_ctx->pix_fmt)) { /* When the video width and height are not multiples of 8, * and there is no size change in the conversion, * a blurry screen will appear on the right side * This problem was discovered in 2012 and * continues to exist in version 4.1.3 in 2019 * This problem can be avoided by increasing SWS_ACCURATE_RND */ if((width & 0x7) || (height & 0x7)) { LV_LOG_WARN("The width(%d) and height(%d) the image " "is not a multiple of 8, " "the decoding speed may be reduced", width, height); swsFlags |= SWS_ACCURATE_RND; } } ffmpeg_ctx->sws_ctx = sws_getContext( width, height, ffmpeg_ctx->video_dec_ctx->pix_fmt, width, height, ffmpeg_ctx->video_dst_pix_fmt, swsFlags, NULL, NULL, NULL); } if(!ffmpeg_ctx->has_alpha) { int lv_linesize = lv_color_format_get_size(LV_COLOR_FORMAT_NATIVE) * width; int dst_linesize = ffmpeg_ctx->video_dst_linesize[0]; if(dst_linesize != lv_linesize) { LV_LOG_WARN("ffmpeg linesize = %d, but lvgl image require %d", dst_linesize, lv_linesize); ffmpeg_ctx->video_dst_linesize[0] = lv_linesize; } } ret = sws_scale( ffmpeg_ctx->sws_ctx, (const uint8_t * const *)(ffmpeg_ctx->video_src_data), ffmpeg_ctx->video_src_linesize, 0, height, ffmpeg_ctx->video_dst_data, ffmpeg_ctx->video_dst_linesize); failed: return ret; } static int ffmpeg_decode_packet(AVCodecContext * dec, const AVPacket * pkt, struct ffmpeg_context_s * ffmpeg_ctx) { int ret = 0; /* submit the packet to the decoder */ ret = avcodec_send_packet(dec, pkt); if(ret < 0) { LV_LOG_ERROR("Error submitting a packet for decoding (%s)", av_err2str(ret)); return ret; } /* get all the available frames from the decoder */ while(ret >= 0) { ret = avcodec_receive_frame(dec, ffmpeg_ctx->frame); if(ret < 0) { /* those two return values are special and mean there is * no output frame available, * but there were no errors during decoding */ if(ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) { return 0; } LV_LOG_ERROR("Error during decoding (%s)", av_err2str(ret)); return ret; } /* write the frame data to output file */ if(dec->codec->type == AVMEDIA_TYPE_VIDEO) { ret = ffmpeg_output_video_frame(ffmpeg_ctx); } av_frame_unref(ffmpeg_ctx->frame); if(ret < 0) { LV_LOG_WARN("ffmpeg_decode_packet ended %d", ret); return ret; } } return 0; } static int ffmpeg_open_codec_context(int * stream_idx, AVCodecContext ** dec_ctx, AVFormatContext * fmt_ctx, enum AVMediaType type) { int ret; int stream_index; AVStream * st; const AVCodec * dec = NULL; AVDictionary * opts = NULL; ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0); if(ret < 0) { LV_LOG_ERROR("Could not find %s stream in input file", av_get_media_type_string(type)); return ret; } else { stream_index = ret; st = fmt_ctx->streams[stream_index]; /* find decoder for the stream */ dec = avcodec_find_decoder(st->codecpar->codec_id); if(dec == NULL) { LV_LOG_ERROR("Failed to find %s codec", av_get_media_type_string(type)); return AVERROR(EINVAL); } /* Allocate a codec context for the decoder */ *dec_ctx = avcodec_alloc_context3(dec); if(*dec_ctx == NULL) { LV_LOG_ERROR("Failed to allocate the %s codec context", av_get_media_type_string(type)); return AVERROR(ENOMEM); } /* Copy codec parameters from input stream to output codec context */ if((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) { LV_LOG_ERROR( "Failed to copy %s codec parameters to decoder context", av_get_media_type_string(type)); return ret; } /* Init the decoders */ if((ret = avcodec_open2(*dec_ctx, dec, &opts)) < 0) { LV_LOG_ERROR("Failed to open %s codec", av_get_media_type_string(type)); return ret; } *stream_idx = stream_index; } return 0; } static int ffmpeg_get_image_header(lv_image_decoder_dsc_t * dsc, lv_image_header_t * header) { int ret = -1; AVFormatContext * fmt_ctx = NULL; AVCodecContext * video_dec_ctx = NULL; AVIOContext * io_ctx = NULL; int video_stream_idx; io_ctx = ffmpeg_open_io_context(&dsc->file); if(io_ctx == NULL) { LV_LOG_ERROR("io_ctx malloc failed"); return ret; } fmt_ctx = avformat_alloc_context(); if(fmt_ctx == NULL) { LV_LOG_ERROR("fmt_ctx malloc failed"); goto failed; } fmt_ctx->pb = io_ctx; fmt_ctx->flags |= AVFMT_FLAG_CUSTOM_IO; /* open input file, and allocate format context */ if(avformat_open_input(&fmt_ctx, (const char *)dsc->src, NULL, NULL) < 0) { LV_LOG_ERROR("Could not open source file %s", (const char *)dsc->src); goto failed; } /* retrieve stream information */ if(avformat_find_stream_info(fmt_ctx, NULL) < 0) { LV_LOG_ERROR("Could not find stream information"); goto failed; } if(ffmpeg_open_codec_context(&video_stream_idx, &video_dec_ctx, fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) { bool has_alpha = ffmpeg_pix_fmt_has_alpha(video_dec_ctx->pix_fmt); /* allocate image where the decoded image will be put */ header->w = video_dec_ctx->width; header->h = video_dec_ctx->height; header->cf = has_alpha ? LV_COLOR_FORMAT_ARGB8888 : LV_COLOR_FORMAT_NATIVE; header->stride = header->w * lv_color_format_get_size(header->cf); ret = 0; } failed: avcodec_free_context(&video_dec_ctx); avformat_close_input(&fmt_ctx); if(io_ctx != NULL) { av_free(io_ctx->buffer); av_free(io_ctx); } return ret; } static int ffmpeg_get_frame_refr_period(struct ffmpeg_context_s * ffmpeg_ctx) { int avg_frame_rate_num = ffmpeg_ctx->video_stream->avg_frame_rate.num; if(avg_frame_rate_num > 0) { int period = 1000 * (int64_t)ffmpeg_ctx->video_stream->avg_frame_rate.den / avg_frame_rate_num; return period; } return -1; } static int ffmpeg_update_next_frame(struct ffmpeg_context_s * ffmpeg_ctx) { int ret = 0; while(1) { /* read frames from the file */ if(av_read_frame(ffmpeg_ctx->fmt_ctx, ffmpeg_ctx->pkt) >= 0) { bool is_image = false; /* check if the packet belongs to a stream we are interested in, * otherwise skip it */ if(ffmpeg_ctx->pkt->stream_index == ffmpeg_ctx->video_stream_idx) { ret = ffmpeg_decode_packet(ffmpeg_ctx->video_dec_ctx, ffmpeg_ctx->pkt, ffmpeg_ctx); is_image = true; } av_packet_unref(ffmpeg_ctx->pkt); if(ret < 0) { LV_LOG_WARN("video frame is empty %d", ret); break; } /* Used to filter data that is not an image */ if(is_image) { break; } } else { ret = -1; break; } } return ret; } static int ffmpeg_lvfs_read(void * ptr, uint8_t * buf, int buf_size) { lv_fs_file_t * file = ptr; uint32_t bytesRead = 0; lv_fs_res_t res = lv_fs_read(file, buf, buf_size, &bytesRead); if(bytesRead == 0) return AVERROR_EOF; /* Let FFmpeg know that we have reached eof */ if(res != LV_FS_RES_OK) return AVERROR_EOF; return bytesRead; } static int64_t ffmpeg_lvfs_seek(void * ptr, int64_t pos, int whence) { lv_fs_file_t * file = ptr; if(whence == SEEK_SET && lv_fs_seek(file, pos, SEEK_SET) == LV_FS_RES_OK) { return pos; } return -1; } static AVIOContext * ffmpeg_open_io_context(lv_fs_file_t * file) { uint8_t * iBuffer = av_malloc(DECODER_BUFFER_SIZE); if(iBuffer == NULL) { LV_LOG_ERROR("iBuffer malloc failed"); return NULL; } AVIOContext * pIOCtx = avio_alloc_context(iBuffer, DECODER_BUFFER_SIZE, /* internal Buffer and its size */ 0, /* bWriteable (1=true,0=false) */ file, /* user data ; will be passed to our callback functions */ ffmpeg_lvfs_read, /* Read callback function */ 0, /* Write callback function */ ffmpeg_lvfs_seek); /* Seek callback function */ if(pIOCtx == NULL) { av_free(iBuffer); return NULL; } return pIOCtx; } static struct ffmpeg_context_s * ffmpeg_open_file(const char * path, bool is_lv_fs_path) { if(path == NULL || lv_strlen(path) == 0) { LV_LOG_ERROR("file path is empty"); return NULL; } struct ffmpeg_context_s * ffmpeg_ctx = lv_malloc_zeroed(sizeof(struct ffmpeg_context_s)); LV_ASSERT_MALLOC(ffmpeg_ctx); if(ffmpeg_ctx == NULL) { LV_LOG_ERROR("ffmpeg_ctx malloc failed"); goto failed; } if(is_lv_fs_path) { const lv_fs_res_t fs_res = lv_fs_open(&(ffmpeg_ctx->lv_file), path, LV_FS_MODE_RD); if(fs_res != LV_FS_RES_OK) { LV_LOG_WARN("Could not open file: %s, res: %d", path, fs_res); lv_free(ffmpeg_ctx); return NULL; } ffmpeg_ctx->io_ctx = ffmpeg_open_io_context(&(ffmpeg_ctx->lv_file)); /* Save the buffer pointer to free it later */ if(ffmpeg_ctx->io_ctx == NULL) { LV_LOG_ERROR("io_ctx malloc failed"); goto failed; } ffmpeg_ctx->fmt_ctx = avformat_alloc_context(); if(ffmpeg_ctx->fmt_ctx == NULL) { LV_LOG_ERROR("fmt_ctx malloc failed"); goto failed; } ffmpeg_ctx->fmt_ctx->pb = ffmpeg_ctx->io_ctx; ffmpeg_ctx->fmt_ctx->flags |= AVFMT_FLAG_CUSTOM_IO; } /* open input file, and allocate format context */ if(avformat_open_input(&(ffmpeg_ctx->fmt_ctx), path, NULL, NULL) < 0) { LV_LOG_ERROR("Could not open source file %s", path); goto failed; } /* retrieve stream information */ if(avformat_find_stream_info(ffmpeg_ctx->fmt_ctx, NULL) < 0) { LV_LOG_ERROR("Could not find stream information"); goto failed; } if(ffmpeg_open_codec_context( &(ffmpeg_ctx->video_stream_idx), &(ffmpeg_ctx->video_dec_ctx), ffmpeg_ctx->fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) { ffmpeg_ctx->video_stream = ffmpeg_ctx->fmt_ctx->streams[ffmpeg_ctx->video_stream_idx]; ffmpeg_ctx->has_alpha = ffmpeg_pix_fmt_has_alpha(ffmpeg_ctx->video_dec_ctx->pix_fmt); ffmpeg_ctx->video_dst_pix_fmt = (ffmpeg_ctx->has_alpha ? AV_PIX_FMT_BGRA : AV_PIX_FMT_TRUE_COLOR); } #if LV_FFMPEG_DUMP_FORMAT /* dump input information to stderr */ av_dump_format(ffmpeg_ctx->fmt_ctx, 0, path, 0); #endif if(ffmpeg_ctx->video_stream == NULL) { LV_LOG_ERROR("Could not find video stream in the input, aborting"); goto failed; } return ffmpeg_ctx; failed: ffmpeg_close(ffmpeg_ctx); return NULL; } static int ffmpeg_image_allocate(struct ffmpeg_context_s * ffmpeg_ctx) { int ret; /* allocate image where the decoded image will be put */ ret = av_image_alloc( ffmpeg_ctx->video_src_data, ffmpeg_ctx->video_src_linesize, ffmpeg_ctx->video_dec_ctx->width, ffmpeg_ctx->video_dec_ctx->height, ffmpeg_ctx->video_dec_ctx->pix_fmt, 4); if(ret < 0) { LV_LOG_ERROR("Could not allocate src raw video buffer"); return ret; } LV_LOG_INFO("alloc video_src_bufsize = %d", ret); ret = av_image_alloc( ffmpeg_ctx->video_dst_data, ffmpeg_ctx->video_dst_linesize, ffmpeg_ctx->video_dec_ctx->width, ffmpeg_ctx->video_dec_ctx->height, ffmpeg_ctx->video_dst_pix_fmt, 4); if(ret < 0) { LV_LOG_ERROR("Could not allocate dst raw video buffer"); return ret; } LV_LOG_INFO("allocate video_dst_bufsize = %d", ret); ffmpeg_ctx->frame = av_frame_alloc(); if(ffmpeg_ctx->frame == NULL) { LV_LOG_ERROR("Could not allocate frame"); return -1; } /* allocate packet, set data to NULL, let the demuxer fill it */ ffmpeg_ctx->pkt = av_packet_alloc(); if(ffmpeg_ctx->pkt == NULL) { LV_LOG_ERROR("av_packet_alloc failed"); return -1; } ffmpeg_ctx->pkt->data = NULL; ffmpeg_ctx->pkt->size = 0; return 0; } static void ffmpeg_close_src_ctx(struct ffmpeg_context_s * ffmpeg_ctx) { avcodec_free_context(&(ffmpeg_ctx->video_dec_ctx)); avformat_close_input(&(ffmpeg_ctx->fmt_ctx)); av_packet_free(&ffmpeg_ctx->pkt); av_frame_free(&(ffmpeg_ctx->frame)); if(ffmpeg_ctx->video_src_data[0] != NULL) { av_free(ffmpeg_ctx->video_src_data[0]); ffmpeg_ctx->video_src_data[0] = NULL; } } static void ffmpeg_close_dst_ctx(struct ffmpeg_context_s * ffmpeg_ctx) { if(ffmpeg_ctx->video_dst_data[0] != NULL) { av_free(ffmpeg_ctx->video_dst_data[0]); ffmpeg_ctx->video_dst_data[0] = NULL; } } static void ffmpeg_close(struct ffmpeg_context_s * ffmpeg_ctx) { if(ffmpeg_ctx == NULL) { LV_LOG_WARN("ffmpeg_ctx is NULL"); return; } sws_freeContext(ffmpeg_ctx->sws_ctx); ffmpeg_close_src_ctx(ffmpeg_ctx); ffmpeg_close_dst_ctx(ffmpeg_ctx); if(ffmpeg_ctx->io_ctx != NULL) { av_free(ffmpeg_ctx->io_ctx->buffer); av_free(ffmpeg_ctx->io_ctx); lv_fs_close(&(ffmpeg_ctx->lv_file)); } lv_free(ffmpeg_ctx); LV_LOG_INFO("ffmpeg_ctx closed"); } static void lv_ffmpeg_player_frame_update_cb(lv_timer_t * timer) { lv_obj_t * obj = (lv_obj_t *)lv_timer_get_user_data(timer); lv_ffmpeg_player_t * player = (lv_ffmpeg_player_t *)obj; if(!player->ffmpeg_ctx) { return; } int has_next = ffmpeg_update_next_frame(player->ffmpeg_ctx); if(has_next < 0) { lv_ffmpeg_player_set_cmd(obj, player->auto_restart ? LV_FFMPEG_PLAYER_CMD_START : LV_FFMPEG_PLAYER_CMD_STOP); if(!player->auto_restart) { lv_obj_send_event((lv_obj_t *)player, LV_EVENT_READY, NULL); } return; } lv_image_cache_drop(lv_image_get_src(obj)); lv_obj_invalidate(obj); } static void lv_ffmpeg_player_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj) { LV_UNUSED(class_p); LV_TRACE_OBJ_CREATE("begin"); lv_ffmpeg_player_t * player = (lv_ffmpeg_player_t *)obj; player->auto_restart = false; player->ffmpeg_ctx = NULL; player->timer = lv_timer_create(lv_ffmpeg_player_frame_update_cb, FRAME_DEF_REFR_PERIOD, obj); lv_timer_pause(player->timer); LV_TRACE_OBJ_CREATE("finished"); } static void lv_ffmpeg_player_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj) { LV_UNUSED(class_p); LV_TRACE_OBJ_CREATE("begin"); lv_ffmpeg_player_t * player = (lv_ffmpeg_player_t *)obj; if(player->timer) { lv_timer_delete(player->timer); player->timer = NULL; } lv_image_cache_drop(lv_image_get_src(obj)); ffmpeg_close(player->ffmpeg_ctx); player->ffmpeg_ctx = NULL; LV_TRACE_OBJ_CREATE("finished"); } #endif /*LV_USE_FFMPEG*/ 解释代码。这里有可以读取本地视频文件并读取帧/GOP的嘛?
09-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北雨南萍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值