• 【转载】使用SDL播放YUV图像数据(转) 分类: ffmpeg-SDL-VLC-Live555 2013-08-28 11:33 757人阅读 评论(0) 收藏


    SDL提供了针对YUV格式数据的直接写屏操作。废话不多说,直接上代码吧

    /**
     * file showyuv.c
     * author: rare
     * date: 2009/12/06
     * email: dux003#163.com
     */

    #include <stdlib.h>
    #include "SDL.h"

    int main(int argc , char* argv[])
    {
        int i = 1;
        int x, y;
        int w = 176;
        int h = 144;
        char c = 'n';

        FILE* fp;
        char filename[64];
        unsigned char* pY;
        unsigned char* pU;
        unsigned char* pV;
        SDL_Rect rect;

        if (SDL_Init(SDL_INIT_VIDEO) < 0)
        {
            fprintf(stderr, "can not initialize SDL:%s ", SDL_GetError());
            exit(1);
        }
        atexit(SDL_Quit);

        SDL_Surface* screen = SDL_SetVideoMode(w, h, 0, 0);
        if (screen == NULL)
        {
            fprintf(stderr, "create surface error! ");
            exit(1);
        }

        SDL_Overlay* verlay = SDL_CreateYUVOverlay(w, h, SDL_YV12_OVERLAY, screen);
        if (overlay == NULL)
        {
            fprintf(stderr, "create overlay error! ");
            exit(1);
        }

        printf("w:%d, h:%d, planes:%d ", overlay->w, overlay->h, overlay->planes);
        printf("pitches:%d, %d, %d ", overlay->pitches[0], overlay->pitches[1], overlay->pitches[2]);

        pY = (unsigned char*)malloc(w*h);
        pU = (unsigned char*)malloc(w*h/4);
        pV = (unsigned char*)malloc(w*h/4);

        while (i<=96)
        {
            SDL_LockSurface(screen);
            SDL_LockYUVOverlay(overlay);

            sprintf(filename, "./carphone/carphone%03d.yuv", i);
            printf("%s ", filename);

            fp = fopen(filename, "rb");
            if (fp == NULL)
            {
                fprintf(stderr, "open file error! ");
                exit(1);
            }

            fread(pY, 1, w*h, fp);
            fread(pU, 1, w*h/4, fp);
            fread(pV, 1, w*h/4, fp);

            memcpy(overlay->pixels[0], pY, w*h);
            memcpy(overlay->pixels[1], pV, w*h/4);
            memcpy(overlay->pixels[2], pU, w*h/4);

            fclose(fp);

            SDL_UnlockYUVOverlay(overlay);
            SDL_UnlockSurface(screen);

            rect.w = w;
            rect.h = h;
            rect.x = rect.y = 0;
            SDL_DisplayYUVOverlay(overlay, &rect);

            SDL_Delay(40);

            i += 1;
        }

        free(pY);
        free(pU);
        free(pV);

        while (c != 'q')
            scanf("%c", &c);

        SDL_FreeYUVOverlay(overlay);
        SDL_FreeSurface(screen);

        return 0;
    }

    其中测试序列是我在网上找的,url链接为:http://www.cipr.rpi.edu/resource/sequences/sequences/qcif/yuv/qcif_yuv_carphone.zip
    代码比较粗糙,有改进意见的欢迎发邮件给我,其它的就免了

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    5.2.9.字符设备驱动代码实践2
    5.2.8.字符设备驱动代码实践1
    5.2.7.字符设备驱动工作原理2
    5.2.6.字符设备驱动工作原理1
    带参宏定义的思考
    重读gets()与is函数的用法
    地址/指针和字符串
    总体来说,require_once 肯定要比 require 性能好
    auto_prepend_file与auto_append_file使用方法
    经验分享:CSS浮动(float,clear)通俗讲解
  • 原文地址:https://www.cnblogs.com/mao0504/p/4706815.html
Copyright © 2020-2023  润新知