Initial WebM release
[profile/ivi/libvpx.git] / examples / decoder_tmpl.c
1 /*
2  *  Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license and patent
5  *  grant that can be found in the LICENSE file in the root of the source
6  *  tree. All contributing project authors may be found in the AUTHORS
7  *  file in the root of the source tree.
8  */
9
10
11 /*
12 @*INTRODUCTION
13  */
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdarg.h>
17 #include <string.h>
18 #define VPX_CODEC_DISABLE_COMPAT 1
19 #include "vpx_decoder.h"
20 #if CONFIG_VP8_DECODER && !defined(interface)
21 #include "vp8dx.h"
22 #define interface (&vpx_codec_vp8_dx_algo)
23 #endif
24 @EXTRA_INCLUDES
25
26
27 #define IVF_FILE_HDR_SZ  (32)
28 #define IVF_FRAME_HDR_SZ (12)
29
30 static unsigned int mem_get_le32(const unsigned char *mem) {
31     return (mem[3] << 24)|(mem[2] << 16)|(mem[1] << 8)|(mem[0]);
32 }
33
34 static void die(const char *fmt, ...) {
35     va_list ap;
36
37     va_start(ap, fmt);
38     vprintf(fmt, ap);
39     if(fmt[strlen(fmt)-1] != '\n')
40         printf("\n");
41     exit(EXIT_FAILURE);
42 }
43
44 @DIE_CODEC
45
46 int main(int argc, char **argv) {
47     FILE            *infile, *outfile;
48     vpx_codec_ctx_t  codec;
49     int              flags = 0, frame_cnt = 0;
50     unsigned char    file_hdr[IVF_FILE_HDR_SZ];
51     unsigned char    frame_hdr[IVF_FRAME_HDR_SZ];
52     unsigned char    frame[256*1024];
53     vpx_codec_err_t  res;
54 @@@@EXTRA_VARS
55
56     (void)res;
57     /* Open files */
58 @@@@USAGE
59     if(!(infile = fopen(argv[1], "rb")))
60         die("Failed to open %s for reading", argv[1]);
61     if(!(outfile = fopen(argv[2], "wb")))
62         die("Failed to open %s for writing", argv[2]);
63
64     /* Read file header */
65     fread(file_hdr, 1, IVF_FILE_HDR_SZ, infile);
66     if(!(file_hdr[0]=='D' && file_hdr[1]=='K' && file_hdr[2]=='I'
67          && file_hdr[3]=='F'))
68         die("%s is not an IVF file.", argv[1]);
69
70     printf("Using %s\n",vpx_codec_iface_name(interface));
71 @@@@DEC_INIT
72
73     /* Read each frame */
74     while(fread(frame_hdr, 1, IVF_FRAME_HDR_SZ, infile) == IVF_FRAME_HDR_SZ) {
75         int               frame_sz = mem_get_le32(frame_hdr);
76         vpx_codec_iter_t  iter = NULL;
77         vpx_image_t      *img;
78
79
80         frame_cnt++;
81         if(frame_sz > sizeof(frame))
82             die("Frame %d data too big for example code buffer", frame_sz);
83         if(fread(frame, 1, frame_sz, infile) != frame_sz)
84             die("Frame %d failed to read complete frame", frame_cnt);
85
86 @@@@@@@@PRE_DECODE
87 @@@@@@@@DECODE
88
89         /* Write decoded data to disk */
90 @@@@@@@@GET_FRAME
91             unsigned int plane, y;
92
93 @@@@@@@@@@@@PROCESS_DX
94         }
95     }
96     printf("Processed %d frames.\n",frame_cnt);
97 @@@@DESTROY
98
99     fclose(outfile);
100     fclose(infile);
101     return EXIT_SUCCESS;
102 }