YUV处理工具:文件拼接

  1. 1、功能说明
  2. 2、增加功能
  3. 3、功能实现(C语言)
  4. THE END!

1、功能说明

YUV文件拼接,可以设置两个YUV文件的帧数,将两个YUV文件的三个分量顺序拼接,只支持YUV420P采样格式。

2、增加功能

Modified: 2018-09-20 Soaringlee

  • 输入YUV文件长度检查,需要拼接的文件长度不能超过YUV本身长度;
  • 可以设置两幅YUV分别重复的次数。

3、功能实现(C语言)

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

int main(int argc, char** argv)
{
    unsigned char* y;
    FILE* fp_yuv1, *fp_yuv2, *fp_yuv3;
    int width, height, len1, len2, picturesize;
    int i, yuv_len1, yuv_len2, dup1, dup2, total_frm_num1, total_frm_num2, dup1_b, dup2_b;

    if (argc < 10)
    {
     printf("Usage: connectYUV.exe src2file src2file dstfile width height len1 len2 dup1 dup2\n\n");
         printf("Notice: the width and height of those two yuv must be same!!!\n");
         printf("Notice: dup1 is the number of replication for srcfile1, and dup2 is the number of replicaiton for srcfile2.\n");
         return -1;
    }
        for(i=0; i<argc;i++)
        {
          printf("%s ", argv[i]);
        }
        printf("\n");

    fp_yuv1 = fopen(argv[1], "rb");
    if (NULL == fp_yuv1)
    {
        printf("ERROR: open %s fail!\n", argv[1]);
        return -1;
    }
    fp_yuv2 = fopen(argv[2], "rb");
    if (NULL == fp_yuv2)
    {
        printf("ERROR: open %s fail!\n", argv[2]);
        return -1;
    }
    fp_yuv3 = fopen(argv[3], "wb");
    if (NULL == fp_yuv3)
    {
        printf("ERROR: open %s fail!\n", argv[3]);
        return -1;
    }

    width = atoi(argv[4]);
    height = atoi(argv[5]);
    len1 = atoi(argv[6]);
    len2 = atoi(argv[7]);
    dup1 = atoi(argv[8]);
    dup2 = atoi(argv[9]);

    picturesize = width *height * 3 / 2; //++YUV420

    // yuv输入长度检查
    fseek(fp_yuv1, 0, SEEK_END);
    yuv_len1 = ftell(fp_yuv1);
    total_frm_num1 = yuv_len1 / picturesize;
    printf("total_frame_num1: %d\n", total_frm_num1);
    fseek(fp_yuv1, 0, SEEK_SET);

    fseek(fp_yuv2, 0, SEEK_END);
    yuv_len2 = ftell(fp_yuv2);
    total_frm_num2 = yuv_len2 / picturesize;
    printf("total_frame_num2: %d\n", total_frm_num2);
    fseek(fp_yuv2, 0, SEEK_SET);

    if( len1 > total_frm_num1 || len2 > total_frm_num2)
    {
        printf("ERROR: len1 > frame_num1 or len2 > frame_num2!\n");
        return -1;
    }

    y = (unsigned char*)malloc(picturesize*sizeof(unsigned char*));
    if (NULL == y)
    {
        printf("ERROR: malloc y fail!\n");
    }
    do{
    for (i = 0; i < len1; i++)
    {
        if (fread(y, 1, picturesize, fp_yuv1) == picturesize)
        {
            fwrite(y, 1, picturesize, fp_yuv3);
        }
    }
        fseek(fp_yuv1, 0, SEEK_SET);//  特别注意,每次读取之后需要重置读指针位置。
        dup1--;
       } while(dup1);

    do{
    for (i = 0; i < len2; i++)
    {
        if (fread(y, 1, picturesize, fp_yuv2) == picturesize)
        {
            fwrite(y, 1, picturesize, fp_yuv3);
        }
    }
       fseek(fp_yuv2, 0, SEEK_SET); // 特别注意,每次读取之后需要重置读指针位置。
       dup2--;
    } while(dup2);

    printf("connect yuv %s and %s ==> %s successfully, total frames: %d\n", argv[1], argv[2], argv[3], len1*dup1_b+len2*dup2_b);

    free(y);
    y = NULL;
    fclose(fp_yuv1);
    fclose(fp_yuv2);
    fclose(fp_yuv3);

    return 0;
}

THE END!


本博文只能阅读,谢绝转载,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 2963033731@qq.com

文章标题:YUV处理工具:文件拼接

字数:566

本文作者:Soaring Lee

发布时间:2018-05-28, 15:13:47

最后更新:2021-06-14, 12:13:44

原始链接:https://soaringleefighting.github.io/2018/05/28/【Codecs系列】YUV处理工具:文件拼接功能/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

×

喜欢就点赞,疼爱就打赏

相册