]> granicus.if.org Git - libvpx/blob - tools_common.c
Merge "Removing vp9_encodeintra.{h, c} files."
[libvpx] / tools_common.c
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include "tools_common.h"
12
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #if defined(_WIN32) || defined(__OS2__)
19 #include <io.h>
20 #include <fcntl.h>
21
22 #ifdef __OS2__
23 #define _setmode    setmode
24 #define _fileno     fileno
25 #define _O_BINARY   O_BINARY
26 #endif
27 #endif
28
29 #define LOG_ERROR(label) do {\
30   const char *l = label;\
31   va_list ap;\
32   va_start(ap, fmt);\
33   if (l)\
34     fprintf(stderr, "%s: ", l);\
35   vfprintf(stderr, fmt, ap);\
36   fprintf(stderr, "\n");\
37   va_end(ap);\
38 } while (0)
39
40
41 FILE *set_binary_mode(FILE *stream) {
42   (void)stream;
43 #if defined(_WIN32) || defined(__OS2__)
44   _setmode(_fileno(stream), _O_BINARY);
45 #endif
46   return stream;
47 }
48
49 void die(const char *fmt, ...) {
50   LOG_ERROR(NULL);
51   usage_exit();
52 }
53
54 void fatal(const char *fmt, ...) {
55   LOG_ERROR("Fatal");
56   exit(EXIT_FAILURE);
57 }
58
59 void warn(const char *fmt, ...) {
60   LOG_ERROR("Warning");
61 }
62
63 uint16_t mem_get_le16(const void *data) {
64   uint16_t val;
65   const uint8_t *mem = (const uint8_t*)data;
66
67   val = mem[1] << 8;
68   val |= mem[0];
69   return val;
70 }
71
72 uint32_t mem_get_le32(const void *data) {
73   uint32_t val;
74   const uint8_t *mem = (const uint8_t*)data;
75
76   val = mem[3] << 24;
77   val |= mem[2] << 16;
78   val |= mem[1] << 8;
79   val |= mem[0];
80   return val;
81 }
82
83 int read_yuv_frame(struct VpxInputContext *input_ctx, vpx_image_t *yuv_frame) {
84   FILE *f = input_ctx->file;
85   struct FileTypeDetectionBuffer *detect = &input_ctx->detect;
86   int plane = 0;
87   int shortread = 0;
88
89   for (plane = 0; plane < 3; ++plane) {
90     uint8_t *ptr;
91     const int w = (plane ? (1 + yuv_frame->d_w) / 2 : yuv_frame->d_w);
92     const int h = (plane ? (1 + yuv_frame->d_h) / 2 : yuv_frame->d_h);
93     int r;
94
95     /* Determine the correct plane based on the image format. The for-loop
96      * always counts in Y,U,V order, but this may not match the order of
97      * the data on disk.
98      */
99     switch (plane) {
100       case 1:
101         ptr = yuv_frame->planes[
102             yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_V : VPX_PLANE_U];
103         break;
104       case 2:
105         ptr = yuv_frame->planes[
106             yuv_frame->fmt == VPX_IMG_FMT_YV12 ? VPX_PLANE_U : VPX_PLANE_V];
107         break;
108       default:
109         ptr = yuv_frame->planes[plane];
110     }
111
112     for (r = 0; r < h; ++r) {
113       size_t needed = w;
114       size_t buf_position = 0;
115       const size_t left = detect->buf_read - detect->position;
116       if (left > 0) {
117         const size_t more = (left < needed) ? left : needed;
118         memcpy(ptr, detect->buf + detect->position, more);
119         buf_position = more;
120         needed -= more;
121         detect->position += more;
122       }
123       if (needed > 0) {
124         shortread |= (fread(ptr + buf_position, 1, needed, f) < needed);
125       }
126
127       ptr += yuv_frame->stride[plane];
128     }
129   }
130
131   return shortread;
132 }