Statistics
| Branch: | Revision:

psnr-tools / Cutter / input.c @ 74add033

History | View | Annotate | Download (2.25 KB)

1 ad2a1304 Csaba Kiraly
/*
2
 *  Copyright (c) 2009 Luca Abeni
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 */
6
7 5b504d3f Csaba Kiraly
#include <sys/types.h>
8
#include <sys/stat.h>
9
#include <fcntl.h>
10
#include <unistd.h>
11 a6862d0c luca
#include <stdint.h>
12 5b504d3f Csaba Kiraly
#include <stdlib.h>
13 681de281 luca
#include <stdio.h>
14 5b504d3f Csaba Kiraly
15 a6862d0c luca
#include "chunk.h"
16 5b504d3f Csaba Kiraly
#include "input.h"
17 681de281 luca
#include "input-avs.h"
18 5b504d3f Csaba Kiraly
19 681de281 luca
struct input_stream_fixed {
20 5b504d3f Csaba Kiraly
  int fd;
21
  int csize;
22
  void *buffer;
23
};
24
25 681de281 luca
struct input_stream {
26
  void *ctx;
27 a6862d0c luca
  int (*read)(void *s_h, struct chunk *c);
28 681de281 luca
  void (*close)(void *s_h);
29 a6862d0c luca
  int id;
30 681de281 luca
};
31
32 a6862d0c luca
static int chunk_read_fixed(void *s_h, struct chunk *c)
33 5b504d3f Csaba Kiraly
{
34 681de281 luca
  struct input_stream_fixed *s = s_h;
35
  int res;
36 5b504d3f Csaba Kiraly
37 681de281 luca
  res = read(s->fd, s->buffer, s->csize);
38
39
  if (res > 0) {
40 a6862d0c luca
    c->frame_len[0] = res;
41
    c->frame[0] = s->buffer;
42
    c->stride_size = 1;
43 681de281 luca
44 a6862d0c luca
    return 1;
45 681de281 luca
  }
46
47 a6862d0c luca
  return 0;
48 681de281 luca
}
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 5b504d3f Csaba Kiraly
  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 681de281 luca
void *input_stream_open(const char *fname, int chunk_size)
86 5b504d3f Csaba Kiraly
{
87 681de281 luca
  struct input_stream *s;
88 5b504d3f Csaba Kiraly
89 681de281 luca
  s = malloc(sizeof(struct input_stream));
90
  if (s == NULL) {
91
    return s;
92
  }
93 a6862d0c luca
  s->id = 0;
94
  if (chunk_size <= 0) {
95 681de281 luca
#ifdef AVS
96 a6862d0c luca
    if (chunk_size == 0) {
97 74add033 Csaba Kiraly
        s->read = chunk_read_avs2;
98 491ea146 Csaba Kiraly
    } else if (chunk_size == -1) {
99 a6862d0c luca
        s->read = chunk_read_avs1;
100 491ea146 Csaba Kiraly
    } else {
101
        s->read = chunk_read_avs2;
102 a6862d0c luca
    }
103 681de281 luca
    s->close = close_input_stream_avs;
104
    s->ctx = input_stream_open_avs(fname, chunk_size);
105
#else
106 a6862d0c luca
    fprintf(stderr, "Chunk Size <= 0 is unsupported!\n");
107 5b504d3f Csaba Kiraly
108 681de281 luca
    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 5b504d3f Csaba Kiraly
  }
115
116 681de281 luca
  return s;
117
}
118
119 a6862d0c luca
int chunk_read(void *s_h, struct chunk *c)
120 681de281 luca
{
121
  struct input_stream *s = s_h;
122
123 a6862d0c luca
  c->id = s->id++;
124
  return s->read(s->ctx, c);
125 5b504d3f Csaba Kiraly
}
126
127 681de281 luca
128 5b504d3f Csaba Kiraly
void input_stream_close(void *s_h)
129
{
130
  struct input_stream *s = s_h;
131
132 681de281 luca
  s->close(s->ctx);
133 5b504d3f Csaba Kiraly
}
134 681de281 luca