FFmpeg學(xué)習(xí)之開發(fā)Mac播放器(二):YUV數(shù)據(jù)轉(zhuǎn)為RGB數(shù)據(jù)并渲染

  • 解碼出的YUV數(shù)據(jù)要轉(zhuǎn)成RGB數(shù)據(jù)然后顯示,我使用AVFilter進行轉(zhuǎn)換而不是sws_scale
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    videoIndex = NSNotFound;
    [self initDecoder];  //初始化解碼器
    [self initFilters];  //初始化過濾器

    self.view.frame = NSRectFromCGRect(CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, pCodecCtx->width, pCodecCtx->height));

    timer = [NSTimer timerWithTimeInterval:1/fps repeats:YES block:^(NSTimer * _Nonnull timer) { //根據(jù)視頻的fps解碼渲染視頻
        [self decodeVideo];  //解碼視頻并渲染
        [self.view setNeedsLayout:YES];  //刷新界面防止畫面撕裂
    }];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode]; //在改變尺寸時保證計時器能調(diào)用
}
初始化解碼器
- (void)initDecoder {
    //av_register_all(); FFmpeg 4.0廢棄

    NSString * videoPath = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp4"];
    pFormatCtx = avformat_alloc_context();

    if ((avformat_open_input(&pFormatCtx, videoPath.UTF8String, NULL, NULL)) != 0) {
        NSLog(@"Could not open input stream");
        return;
    }

    if ((avformat_find_stream_info(pFormatCtx, NULL)) < 0) {
        NSLog(@"Could not find stream information");
        return;
    }

    for (NSInteger i = 0; i < pFormatCtx->nb_streams; i++) {
        if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            videoIndex = i;  //視頻流的索引
            videoDuration = pFormatCtx->streams[i]->duration * av_q2d(pFormatCtx->streams[i]->time_base); //計算視頻時長
            _totalTimeLabel.stringValue = [NSString stringWithFormat:@"%.2ld:%.2ld", videoDuration/60, videoDuration%60];
            if (pFormatCtx->streams[i]->avg_frame_rate.den && pFormatCtx->streams[i]->avg_frame_rate.num) {
                fps = av_q2d(pFormatCtx->streams[i]->avg_frame_rate);  //計算視頻fps
            } else {
                fps = 30;
            }
            break;
        }
    }

    if (videoIndex == NSNotFound) {
        NSLog(@"Did not find a video stream");
        return;
    }

    // FFmpeg 3.1 以上AVStream::codec被替換為AVStream::codecpar
    pCodec = avcodec_find_decoder(pFormatCtx->streams[videoIndex]->codecpar->codec_id);
    pCodecCtx = avcodec_alloc_context3(pCodec);
    avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoIndex]->codecpar);

    if (pCodec == NULL) {
        NSLog(@"Could not open codec");
        return;
    }

    if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
        NSLog(@"Could not open codec");
        return;
    }

    av_dump_format(pFormatCtx, 0, videoPath.UTF8String, 0);
}
初始化過濾器
- (void)initFilters {

//    avfilter_register_all();  //FFmpeg 4.0廢棄

    char args[512];

    AVFilterInOut * inputs = avfilter_inout_alloc();
    AVFilterInOut * outputs = avfilter_inout_alloc();
    AVFilterGraph * filterGraph = avfilter_graph_alloc();

    const AVFilter * buffer = avfilter_get_by_name("buffer");
    const AVFilter * bufferSink = avfilter_get_by_name("buffersink");
    if (!buffer || !bufferSink) {
        NSLog(@"filter not found");
        return;
    }
    AVRational time_base = pFormatCtx->streams[videoIndex]->time_base;
    //視頻的描述字符串,這些屬性都是必須的否則會創(chuàng)建失敗
    snprintf(args, sizeof(args), "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d", pCodecCtx->width,pCodecCtx->height,pCodecCtx->pix_fmt,
             time_base.num, time_base.den,pCodecCtx->sample_aspect_ratio.num,pCodecCtx->sample_aspect_ratio.den);
    NSInteger ret = avfilter_graph_create_filter(&buffer_ctx, buffer, "in", args, NULL, filterGraph);
    if (ret < 0) {
        NSLog(@"can not create buffer source");
        return;
    }
    ret = avfilter_graph_create_filter(&bufferSink_ctx, bufferSink, "out", NULL, NULL, filterGraph);
    if (ret < 0) {
        NSLog(@"can not create buffer sink");
        return;
    }
    enum AVPixelFormat format[] = {AV_PIX_FMT_RGB24};  //想要轉(zhuǎn)換的格式
    ret = av_opt_set_bin(bufferSink_ctx, "pix_fmts", (uint8_t *)&format, sizeof(AV_PIX_FMT_RGB24), AV_OPT_SEARCH_CHILDREN);
    if (ret < 0) {
        NSLog(@"set bin error");
        return;
    }

    outputs->name = av_strdup("in");
    outputs->filter_ctx = buffer_ctx;
    outputs->pad_idx = 0;
    outputs->next = NULL;

    inputs->name = av_strdup("out");
    inputs->filter_ctx = bufferSink_ctx;
    inputs->pad_idx = 0;
    inputs->next = NULL;

    ret = avfilter_graph_parse_ptr(filterGraph, "null", &inputs, &outputs, NULL);  //只轉(zhuǎn)換格式filter名稱輸入null
    if (ret < 0) {
        NSLog(@"parse error");
        return;
    }

    ret = avfilter_graph_config(filterGraph, NULL);
    if (ret < 0) {
        NSLog(@"config error");
        return;
    }

    avfilter_inout_free(&inputs);
    avfilter_inout_free(&outputs);
}
解碼并渲染視頻
- (void)decodeVideo {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  //在全局隊列中解碼
        AVPacket * packet = av_packet_alloc();
        if (av_read_frame(self->pFormatCtx, packet) >= 0) {
            if (packet->stream_index == self->videoIndex) {  //解碼視頻流
                //FFmpeg 3.0之后avcodec_send_packet和avcodec_receive_frame成對出現(xiàn)用于解碼,包括音頻和視頻的解碼,avcodec_decode_video2和avcodec_decode_audio4被廢棄
                NSInteger ret = avcodec_send_packet(self->pCodecCtx, packet);
                if (ret < 0) {
                    NSLog(@"send packet error");
                    av_packet_free(&packet);
                    return;
                }
                AVFrame * frame = av_frame_alloc();
                ret = avcodec_receive_frame(self->pCodecCtx, frame);
                if (ret < 0) {
                    NSLog(@"receive frame error");
                    av_frame_free(&frame);
                    return;
                }
                //把解碼出的frame傳入filter中進行格式轉(zhuǎn)換
                ret = av_buffersrc_add_frame_flags(self->buffer_ctx, frame, 0);
                if (ret < 0) {
                    NSLog(@"add frame error");
                    return;
                }
                //將轉(zhuǎn)換好的rgbFrame取出來
                AVFrame * rgbFrame = av_frame_alloc();
                ret = av_buffersink_get_frame(self->bufferSink_ctx, rgbFrame);
                if (ret < 0) {
                    NSLog(@"get frame error");
                    return;
                }
                /*
                 frame中data存放解碼出的yuv數(shù)據(jù),data[0]中是y數(shù)據(jù),data[1]中是u數(shù)據(jù),data[2]中是v數(shù)據(jù),linesize對應(yīng)的數(shù)據(jù)長度
                 rgb數(shù)據(jù)全部都存放在frame的data[0]中
                 */
                float time = packet->pts * av_q2d(self->pFormatCtx->streams[self->videoIndex]->time_base);  //計算當(dāng)前幀時間
                av_packet_free(&packet);
                av_frame_free(&frame);
                //將frame中的RGB數(shù)據(jù)轉(zhuǎn)成NSImage顯示
                CFDataRef data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, rgbFrame->data[0], rgbFrame->linesize[0] * self->pCodecCtx->height, kCFAllocatorNull);
                if (CFDataGetLength(data) != 0) {
                    CGDataProviderRef provider = CGDataProviderCreateWithCFData(data);
                    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
                    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
                    CGImageRef cgImage = CGImageCreate(self->pCodecCtx->width, self->pCodecCtx->height, 8, 24, rgbFrame->linesize[0], colorSpace, bitmapInfo, provider, NULL, YES, kCGRenderingIntentDefault);
                    NSImage * image = [[NSImage alloc] initWithCGImage:cgImage size:NSSizeFromCGSize(CGSizeMake(self->pCodecCtx->width, self->pCodecCtx->height))];
                    CGImageRelease(cgImage);
                    CGDataProviderRelease(provider);
                    CGColorSpaceRelease(colorSpace);
                    av_frame_free(&rgbFrame);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        self.label.stringValue = [NSString stringWithFormat:@"%.2d:%.2d", (int)time/60, (int)time%60];
                        self.imageView.image = image;
                        self.slider.floatValue = time / (float)self->videoDuration;
                    });
                }
            }
        } else {
            avcodec_free_context(&self->pCodecCtx);
            avformat_close_input(&self->pFormatCtx);
            avformat_free_context(self->pFormatCtx);
            [self->timer invalidate];
        }
    });
}
播放畫面.png

雖然能播放視頻但是有一個問題,根據(jù)視頻的FPS調(diào)用定時器解碼顯示視頻幀率并不能達到視頻的幀率,因為解碼,轉(zhuǎn)換格式,合成圖片這些操作費時,而且CPU占用率也很高,這些問題后面會解決。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 教程一:視頻截圖(Tutorial 01: Making Screencaps) 首先我們需要了解視頻文件的一些基...
    90后的思維閱讀 4,990評論 0 3
  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,228評論 3 119
  • FFmpeg 介紹 FFmpeg是一套可以用來記錄、轉(zhuǎn)換數(shù)字音頻、視頻,并能將其轉(zhuǎn)化為流的開源計算機程序。采用LG...
    Y了個J閱讀 11,589評論 0 28
  • 很多時候你一個人習(xí)慣了,就無法給與囑托,你習(xí)慣了如風(fēng)般不結(jié)伴穿梭,影子都沒留片刻,很多時候你一個人常常是,只考慮一...
    小虎仔啦啦啦閱讀 403評論 0 0
  • 反邪防邪 人人有責(zé) (對口詞) 甲:全黨動員 全民參與 乙:鏟除邪教 構(gòu)建...
    張世林zsl閱讀 607評論 0 1

友情鏈接更多精彩內(nèi)容