| 1 | #include <stdio.h>
|
|---|
| 2 | #include <stdlib.h>
|
|---|
| 3 |
|
|---|
| 4 | #include <libavformat/avformat.h>
|
|---|
| 5 | #include <libavcodec/avcodec.h>
|
|---|
| 6 | #include <libavutil/avutil.h>
|
|---|
| 7 | #include <libavutil/mastering_display_metadata.h>
|
|---|
| 8 |
|
|---|
| 9 | static void decode(const char *url, const char *name) {
|
|---|
| 10 | printf("Decode by %s\n", name);
|
|---|
| 11 |
|
|---|
| 12 | AVFormatContext *fmt_ctx = NULL;
|
|---|
| 13 | int ret;
|
|---|
| 14 |
|
|---|
| 15 | if (avformat_open_input(&fmt_ctx, url, NULL, NULL) < 0) {
|
|---|
| 16 | fprintf(stderr, "avformat_open_input failed\n");
|
|---|
| 17 | exit(1);
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
|
|---|
| 21 | fprintf(stderr, "avformat_find_stream_info failed\n");
|
|---|
| 22 | exit(1);
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | AVCodec *codec = avcodec_find_decoder_by_name(name);
|
|---|
| 26 | if (!codec) {
|
|---|
| 27 | fprintf(stderr, "avcodec_find_decoder_by_name failed\n");
|
|---|
| 28 | exit(1);
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | AVCodecContext *avctx = avcodec_alloc_context3(codec);
|
|---|
| 32 | if (!avctx) {
|
|---|
| 33 | fprintf(stderr, "avcodec_alloc_context3 failed\n");
|
|---|
| 34 | exit(1);
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | if (avcodec_parameters_to_context(avctx, fmt_ctx->streams[0]->codecpar) < 0) {
|
|---|
| 38 | fprintf(stderr, "avcodec_parameters_to_context failed\n");
|
|---|
| 39 | exit(1);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | avctx->pkt_timebase = fmt_ctx->streams[0]->time_base;
|
|---|
| 43 |
|
|---|
| 44 | if (avcodec_open2(avctx, codec, NULL) < 0) {
|
|---|
| 45 | fprintf(stderr, "avcodec_open2 failed\n");
|
|---|
| 46 | exit(1);
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | AVPacket *pkt = av_packet_alloc();
|
|---|
| 50 | if (!pkt) {
|
|---|
| 51 | fprintf(stderr, "av_packet_alloc failed\n");
|
|---|
| 52 | exit(1);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | AVFrame *frame = av_frame_alloc();
|
|---|
| 56 | if (!frame) {
|
|---|
| 57 | fprintf(stderr, "av_frame_alloc failed\n");
|
|---|
| 58 | exit(1);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | while (av_read_frame(fmt_ctx, pkt) >= 0) {
|
|---|
| 62 | ret = avcodec_send_packet(avctx, pkt);
|
|---|
| 63 | if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
|
|---|
| 64 | fprintf(stderr, "avcodec_send_packet failed\n");
|
|---|
| 65 | exit(1);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | ret = avcodec_receive_frame(avctx, frame);
|
|---|
| 69 | if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
|
|---|
| 70 | fprintf(stderr, "avcodec_receive_frame failed\n");
|
|---|
| 71 | exit(1);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | if (ret >= 0) {
|
|---|
| 75 | printf("Picture type: %c\n", av_get_picture_type_char(frame->pict_type));
|
|---|
| 76 |
|
|---|
| 77 | const AVFrameSideData *side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
|
|---|
| 78 | if (side_data) {
|
|---|
| 79 | const AVMasteringDisplayMetadata *mastering_display = (const AVMasteringDisplayMetadata *)side_data->data;
|
|---|
| 80 | if (mastering_display->has_luminance) {
|
|---|
| 81 | printf("Min luminance: %f\n", av_q2d(mastering_display->min_luminance));
|
|---|
| 82 | printf("Max luminance: %f\n", av_q2d(mastering_display->max_luminance));
|
|---|
| 83 | }
|
|---|
| 84 | } else {
|
|---|
| 85 | printf("No side data in the frame\n");
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | printf("\n");
|
|---|
| 89 | break;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | av_packet_unref(pkt);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | avformat_close_input(&fmt_ctx);
|
|---|
| 96 | avcodec_free_context(&avctx);
|
|---|
| 97 | av_packet_free(&pkt);
|
|---|
| 98 | av_frame_free(&frame);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | int main(int argc, char **argv) {
|
|---|
| 102 | decode(argv[1], "hevc");
|
|---|
| 103 | decode(argv[1], "hevc_cuvid");
|
|---|
| 104 |
|
|---|
| 105 | return 0;
|
|---|
| 106 | }
|
|---|