• Yunqing Wang's avatar
    Multiple-resolution encoder · aa7335e6
    Yunqing Wang authored
    The example encoder down-samples the input video frames a number of
    times with a down-sampling factor, and then encodes and outputs
    bitstreams with different resolutions.
    
    Support arbitrary down-sampling factor, and down-sampling factor
    can be different for each encoding level.
    
    For example, the encoder can be tested as follows.
    1. Configure with multi-resolution encoding enabled:
    ../libvpx/configure --target=x86-linux-gcc --disable-codecs
    --enable-vp8 --enable-runtime_cpu_detect --enable-debug
    --disable-install-docs --enable-error-concealment
    --enable-multi-res-encoding
    2. Run make
    3. Encode:
    If input video is 1280x720, run:
    ./vp8_multi_resolution_encoder 1280 720 input.yuv 1.ivf 2.ivf 3.ivf 1
    (output: 1.ivf(1280x720); 2.ivf(640x360); 3.ivf(320x180).
    The last parameter is set to 1/0 to show/not show PSNR.)
    4. Decode:
    ./simple_decoder 1.ivf 1.yuv
    ./simple_decoder 2.ivf 2.yuv
    ./simple_decoder 3.ivf 3.yuv
    5. View video:
    mplayer 1.yuv -demuxer rawvideo -rawvideo w=1280:h=720 -loop 0 -fps 30
    mplayer 2.yuv -demuxer rawvideo -rawvideo w=640:h=360 -loop 0 -fps 30
    mplayer 3.yuv -demuxer rawvideo -rawvideo w=320:h=180 -loop 0 -fps 30
    
    The encoding parameters can be modified in vp8_multi_resolution_encoder.c,
    for example, target bitrate, frame rate...
    
    Modified API. John helped a lot with that. Thanks!
    
    Change-Id: I03be9a51167eddf94399f92d269599fb3f3d54f5
    aa7335e6
ivfdec.c 3.07 KiB
/*
 *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vpx_ports/mem_ops.h"
#include "./ivfdec.h"
static const char *IVF_SIGNATURE = "DKIF";
static void fix_framerate(int *num, int *den) {
  // Some versions of vpxenc used 1/(2*fps) for the timebase, so
  // we can guess the framerate using only the timebase in this
  // case. Other files would require reading ahead to guess the
  // timebase, like we do for webm.
  if (*num < 1000) {
    // Correct for the factor of 2 applied to the timebase in the encoder.
    if (*num & 1)
      *den *= 2;
    else
      *num /= 2;
  } else {
    // Don't know FPS for sure, and don't have readahead code
    // (yet?), so just default to 30fps.
    *num = 30;
    *den = 1;
int file_is_ivf(struct VpxInputContext *input_ctx) {
  char raw_hdr[32];
  int is_ivf = 0;
  if (fread(raw_hdr, 1, 32, input_ctx->file) == 32) {
    if (memcmp(IVF_SIGNATURE, raw_hdr, 4) == 0) {
      is_ivf = 1;
      if (mem_get_le16(raw_hdr + 4) != 0) {
        fprintf(stderr, "Error: Unrecognized IVF version! This file may not"
                " decode properly.");
      input_ctx->fourcc = mem_get_le32(raw_hdr + 8);
      input_ctx->width = mem_get_le16(raw_hdr + 12);
      input_ctx->height = mem_get_le16(raw_hdr + 14);
      input_ctx->framerate.numerator = mem_get_le32(raw_hdr + 16);
      input_ctx->framerate.denominator = mem_get_le32(raw_hdr + 20);
      fix_framerate(&input_ctx->framerate.numerator,
                    &input_ctx->framerate.denominator);
  if (!is_ivf) {
    rewind(input_ctx->file);
    input_ctx->detect.buf_read = 0;
  } else {
    input_ctx->detect.position = 4;
  return is_ivf;
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
int ivf_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read, size_t *buffer_size) { char raw_header[IVF_FRAME_HDR_SZ] = {0}; size_t frame_size = 0; if (fread(raw_header, IVF_FRAME_HDR_SZ, 1, infile) != 1) { if (!feof(infile)) warn("Failed to read frame size\n"); } else { frame_size = mem_get_le32(raw_header); if (frame_size > 256 * 1024 * 1024) { warn("Read invalid frame size (%u)\n", (unsigned int)frame_size); frame_size = 0; } if (frame_size > *buffer_size) { uint8_t *new_buffer = realloc(*buffer, 2 * frame_size); if (new_buffer) { *buffer = new_buffer; *buffer_size = 2 * frame_size; } else { warn("Failed to allocate compressed data buffer\n"); frame_size = 0; } } } if (!feof(infile)) { if (fread(*buffer, 1, frame_size, infile) != frame_size) { warn("Failed to read full frame\n"); return 1; } *bytes_read = frame_size; return 0; } return 1; }