Statistics
| Branch: | Revision:

psnr-tools / Cutter / input.c @ master

History | View | Annotate | Download (2.25 KB)

1
/*
2
 *  Copyright (c) 2009 Luca Abeni
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 */
6

    
7
#include <sys/types.h>
8
#include <sys/stat.h>
9
#include <fcntl.h>
10
#include <unistd.h>
11
#include <stdint.h>
12
#include <stdlib.h>
13
#include <stdio.h>
14

    
15
#include "chunk.h"
16
#include "input.h"
17
#include "input-avs.h"
18

    
19
struct input_stream_fixed {
20
  int fd;
21
  int csize;
22
  void *buffer;
23
};
24

    
25
struct input_stream {
26
  void *ctx;
27
  int (*read)(void *s_h, struct chunk *c);
28
  void (*close)(void *s_h);
29
  int id;
30
};
31

    
32
static int chunk_read_fixed(void *s_h, struct chunk *c)
33
{
34
  struct input_stream_fixed *s = s_h;
35
  int res;
36

    
37
  res = read(s->fd, s->buffer, s->csize);
38

    
39
  if (res > 0) {
40
    c->frame_len[0] = res;
41
    c->frame[0] = s->buffer;
42
    c->stride_size = 1;
43

    
44
    return 1;
45
  }
46

    
47
  return 0;
48
}
49

    
50
static void input_stream_close_fixed(void *s_h)
51
{
52
  struct input_stream_fixed *s = s_h;
53

    
54
  close(s->fd);
55
  free(s->buffer);
56
  free(s);
57
}
58

    
59
static void *input_stream_open_fixed(const char *fname, int chunk_size)
60
{
61
  struct input_stream_fixed *s;
62

    
63
  s = malloc(sizeof(struct input_stream_fixed));
64
  if (s == NULL) {
65
    return s;
66
  }
67
  s->buffer = malloc(chunk_size);
68
  if (s->buffer == NULL) {
69
    free(s);
70

    
71
    return NULL;
72
  }
73
  s->fd = open(fname, 0);
74
  if (s->fd < 0) {
75
    free(s->buffer);
76
    free(s);
77

    
78
    return NULL;
79
  }
80
  s->csize = chunk_size;
81

    
82
  return s;
83
}
84

    
85
void *input_stream_open(const char *fname, int chunk_size)
86
{
87
  struct input_stream *s;
88

    
89
  s = malloc(sizeof(struct input_stream));
90
  if (s == NULL) {
91
    return s;
92
  }
93
  s->id = 0;
94
  if (chunk_size <= 0) {
95
#ifdef AVS
96
    if (chunk_size == 0) {
97
        s->read = chunk_read_avs2;
98
    } else if (chunk_size == -1) {
99
        s->read = chunk_read_avs1;
100
    } else {
101
        s->read = chunk_read_avs2;
102
    }
103
    s->close = close_input_stream_avs;
104
    s->ctx = input_stream_open_avs(fname, chunk_size);
105
#else
106
    fprintf(stderr, "Chunk Size <= 0 is unsupported!\n");
107

    
108
    return NULL;
109
#endif
110
  } else {
111
    s->read = chunk_read_fixed;
112
    s->close = input_stream_close_fixed;
113
    s->ctx = input_stream_open_fixed(fname, chunk_size);
114
  }
115

    
116
  return s;
117
}
118

    
119
int chunk_read(void *s_h, struct chunk *c)
120
{
121
  struct input_stream *s = s_h;
122

    
123
  c->id = s->id++;
124
  return s->read(s->ctx, c);
125
}
126

    
127

    
128
void input_stream_close(void *s_h)
129
{
130
  struct input_stream *s = s_h;
131

    
132
  s->close(s->ctx);
133
}
134

    
135