File: ffsox_frame_reader.c

package info (click to toggle)
bs1770gain 0.4.12-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,832 kB
  • sloc: ansic: 9,380; sh: 1,288; makefile: 212
file content (221 lines) | stat: -rwxr-xr-x 5,024 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * ffsox_frame_reader.c
 * Copyright (C) 2014 Peter Belkner <pbelkner@users.sf.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2.0 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301  USA
 */
#include <ffsox_priv.h>

static frame_reader_vmt_t vmt;

int ffsox_frame_reader_create(frame_reader_t *fr, source_t *si,
    int stream_index, int stereo, double drc)
{
  AVDictionary *opts=NULL;
  char buf[32];

  // initialize the base calss.
  if (ffsox_packet_consumer_create(&fr->packet_consumer,si,stream_index)<0) {
    DMESSAGE("creating packet consumer");
    goto base;
  }

  // set the vmt.
  fr->vmt=ffsox_frame_reader_get_vmt();

  // find a decoder.
  if (NULL==(fr->si.codec=ffsox_find_decoder(fr->si.cc->codec_id))) {
    DMESSAGE("finding decoder");
    goto find;
  }

  // if we want to have stereo:
  if (stereo)
    fr->si.cc->request_channel_layout=AV_CH_LAYOUT_STEREO;

  if (AV_CODEC_ID_AC3==fr->si.cc->codec_id) {
    // avoid dynamic range compression.
    sprintf(buf,"%0.2f",drc);
    av_dict_set(&opts,"drc_scale",buf,0);
  }

  if (avcodec_open2(fr->si.cc,fr->si.codec,&opts)<0) {
    av_dict_free(&opts);
    DMESSAGE("opening decoder");
    goto open;
  }

  av_dict_free(&opts);

  if (ffsox_frame_create(&fr->fo)<0)
    goto frame;

  fr->next=NULL;
  memset(&fr->pkt,0,sizeof fr->pkt);

  return 0;
// close:
  ffsox_frame_cleanup(&fr->fo);
frame:
  avcodec_close(fr->si.cc);
open:
find:
  vmt.parent->cleanup(&fr->packet_consumer);
base:
  return -1;
}

frame_reader_t *ffsox_frame_reader_new(source_t *si, int stream_index,
    int stereo, double drc)
{
  frame_reader_t *fr;

  if (NULL==(fr=MALLOC(sizeof *fr))) {
    DMESSAGE("allocating frame reader");
    goto malloc;
  }

  if (ffsox_frame_reader_create(fr,si,stream_index,stereo,drc)<0) {
    DMESSAGE("creating frame reader");
    goto create;
  }

  return fr;
create:
  FREE(fr);
malloc:
  return NULL;
}

////////
static void frame_reader_cleanup(frame_reader_t *fr)
{
  ffsox_frame_cleanup(&fr->fo);
  avcodec_close(fr->si.cc);
  vmt.parent->cleanup(&fr->packet_consumer);
}

static node_t *frame_reader_next(frame_reader_t *n)
{
  return NULL==n->next?NULL:&n->next->node;
}

static int frame_reader_next_set_frame(frame_reader_t *n, frame_t *fo)
{
  if (NULL!=n->next) {
    if (NULL==fo)
      n->next->state=STATE_FLUSH;

    if (n->next->vmt->set_frame(n->next,fo)<0) {
      DMESSAGE("setting frame");
      return -1;
    }
  }

  return MACHINE_PUSH;
}

#define FRAME_READER_SKIP_ERROR
static int frame_reader_run(frame_reader_t *n)
{
  AVCodecContext *cc=n->si.cc;
  frame_t *fo=&n->fo;
  AVFrame *frame=fo->frame;
  AVPacket *pkt=&n->pkt;
  int got_frame,size;

  switch (n->state) {
  case STATE_RUN:
    while (0<pkt->size) {
      if (0ll<fo->nb_samples.frame) {
        DMESSAGE("frame not consumed");
        return -1;
      }

      if ((size=avcodec_decode_audio4(cc,frame,&got_frame,pkt))<0) {
#if defined (FRAME_READER_SKIP_ERROR) // {
        // skip the package.
        DMESSAGE("decoding audio, skipping audio package");
        pkt->size=0;
        return 0;
#else // } {
        DMESSAGE("decoding audio");
        return -1;
#endif // }
      }

      pkt->size-=size;
      pkt->data+=size;

      if (0!=got_frame)
        return frame_reader_next_set_frame(n,fo);
    }

    return MACHINE_POP;
  case STATE_FLUSH:
    pkt->size=0;
    pkt->data=NULL;

    if (avcodec_decode_audio4(cc,frame,&got_frame,pkt)<0) {
      DMESSAGE("decoding audio");
      return -1;
    }

    if (0==got_frame) {
      n->state=STATE_END;

      return frame_reader_next_set_frame(n,NULL);
    }
    else
      return frame_reader_next_set_frame(n,fo);
  case STATE_END:
    return MACHINE_POP;
  default:
    DMESSAGE("illegal read decode state");
    return -1;
  }
}

static int frame_reader_set_packet(frame_reader_t *n, AVPacket *pkt)
{
  if (NULL==pkt) {
    n->state=STATE_FLUSH;
    n->pkt.size=0;
    n->pkt.data=NULL;
  }
  else
    n->pkt=*pkt;

  return 0;
}

const frame_reader_vmt_t *ffsox_frame_reader_get_vmt(void)
{
  const packet_consumer_vmt_t *parent;

  if (NULL==vmt.parent) {
    parent=ffsox_packet_consumer_get_vmt();
    vmt.packet_consumer=*parent;
    vmt.parent=parent;
    vmt.name="frame_reader";
    vmt.cleanup=frame_reader_cleanup;
    vmt.next=frame_reader_next;
    vmt.run=frame_reader_run;
    vmt.set_packet=frame_reader_set_packet;
  }

  return &vmt;
}