【问题标题】:How to write bitmaps as frames to Ogg Theora in C\C++?如何在 C\C++ 中将位图作为帧写入 Ogg Theora?
【发布时间】:2010-12-16 17:12:38
【问题描述】:

如何在 C\C++ 中将位图作为帧写入 Ogg Theora?

一些带有源代码的示例将是 grate!)

【问题讨论】:

    标签: c++ c ogg-theora


    【解决方案1】:

    这是libtheora APIexample code

    这是一个micro howto,它展示了如何使用 theora 二进制文件。当编码器读取视频的原始、未压缩的“yuv4mpeg”数据时,您也可以通过将视频帧传送到编码器来从您的应用程序中使用它。

    【讨论】:

      【解决方案2】:

      在此处作为代码示例发布整个解决方案有点冗长,但如果您从 Xiph.org 下载 libtheora,则有一个示例 png2theora。我将要提到的所有库函数都可以在 Xiph.org 上的 theora 和 ogg 文档中找到。

      1. 调用 th_info_init() 来初始化 th_info 结构,然后通过在其中分配适当的成员来设置输出参数。
      2. 在调用 th_encode_alloc() 时使用该结构来获取编码器上下文
      3. 使用 ogg_stream_init() 初始化 ogg 流
      4. 使用 th_comment_init 初始化一个空白的 th_comment 结构

      遍历以下内容:

      1. 使用编码器上下文、空白注释结构和 ogg_packet 调用 th_encode_flushheader。
      2. 使用 ogg_stream_packetin() 将生成的数据包发送到 ogg 流

      直到 th_encode_flushheader 返回 0(或错误代码)

      现在,重复调用 ogg_stream_pageout(),每次将 page.header 和 page.body 写入输出文件,直到返回 0。现在调用 ogg_stream_flush 并将生成的页面写入文件。

      您现在可以将帧写入编码器。这是我的做法:

      int theora_write_frame(int outputFd, unsigned long w, unsigned long h, unsigned char *yuv_y, unsigned char *yuv_u, unsigned char *yuv_v, int last)
      {
        th_ycbcr_buffer ycbcr;
        ogg_packet op;
        ogg_page og;
      
        unsigned long yuv_w;
        unsigned long yuv_h;
      
        /* Must hold: yuv_w >= w */
        yuv_w = (w + 15) & ~15;
        /* Must hold: yuv_h >= h */
        yuv_h = (h + 15) & ~15;
      
        //Fill out the ycbcr buffer
        ycbcr[0].width = yuv_w;
        ycbcr[0].height = yuv_h;
        ycbcr[0].stride = yuv_w;
        ycbcr[1].width = yuv_w;
        ycbcr[1].stride = ycbcr[1].width;
        ycbcr[1].height = yuv_h;
        ycbcr[2].width = ycbcr[1].width;
        ycbcr[2].stride = ycbcr[1].stride;
        ycbcr[2].height = ycbcr[1].height;
      
        if(encoderInfo->pixel_fmt == TH_PF_420)
        {
          //Chroma is decimated by 2 in both directions
          ycbcr[1].width = yuv_w >> 1;
          ycbcr[2].width = yuv_w >> 1;
          ycbcr[1].height = yuv_h >> 1;
          ycbcr[2].height = yuv_h >> 1;
        }else if(encoderInfo->pixel_fmt == TH_PF_422)
        {
          ycbcr[1].width = yuv_w >> 1;
          ycbcr[2].width = yuv_w >> 1;
        }else if(encoderInfo->pixel_fmt != TH_PF_422)
        {
          //Then we have an unknown pixel format
          //We don't know how long the arrays are!
          fprintf(stderr, "[theora_write_frame] Unknown pixel format in writeFrame!\n");
          return -1;
        }
      
        ycbcr[0].data = yuv_y;
        ycbcr[1].data = yuv_u;
        ycbcr[2].data = yuv_v;
      
        /* Theora is a one-frame-in,one-frame-out system; submit a frame
           for compression and pull out the packet */
        if(th_encode_ycbcr_in(encoderContext, ycbcr)) {
          fprintf(stderr, "[theora_write_frame] Error: could not encode frame\n");
          return -1;
        }
      
        if(!th_encode_packetout(encoderContext, last, &op)) {
          fprintf(stderr, "[theora_write_frame] Error: could not read packets\n");
          return -1;
        }
      
        ogg_stream_packetin(&theoraStreamState, &op);
        ssize_t bytesWritten = 0;
        int pagesOut = 0;
        while(ogg_stream_pageout(&theoraStreamState, &og)) {
          pagesOut ++;
          bytesWritten = write(outputFd, og.header, og.header_len);
          if(bytesWritten != og.header_len)
          {
            fprintf(stderr, "[theora_write_frame] Error: Could not write to file\n");
            return -1;
          }
          bytesWritten = write(outputFd, og.body, og.body_len);
          if(bytesWritten != og.body_len)
          {
            bytesWritten = fprintf(stderr, "[theora_write_frame] Error: Could not write to file\n");
            return -1;
          }
        }
        return pagesOut;
      }
      

      其中 encoderInfo 是用于初始化编码器的 th_info 结构(我的数据部分是静态的)。

      在您的最后一帧上,在 th_encode_packetout() 上设置最后一帧将确保流正确终止。

      完成后,请确保进行清理(主要是关闭 fds)。 th_info_clear() 将清除 th_info 结构,而 th_encode_free() 将释放您的编码器上下文。

      显然,您需要先将位图转换为 YUV 平面,然后才能将它们传递给 theora_write_frame()。

      希望这会有所帮助。祝你好运!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-16
        • 1970-01-01
        • 1970-01-01
        • 2013-04-07
        • 2023-04-08
        • 1970-01-01
        • 1970-01-01
        • 2021-06-30
        相关资源
        最近更新 更多