Move things round a bit.
[dcpomatic.git] / src / lib / delay_line.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <stdint.h>
21 #include <cstring>
22 #include <algorithm>
23 #include <iostream>
24 #include "delay_line.h"
25
26 using namespace std;
27
28 /** Construct a DelayLine delaying by some number of bytes.
29  *  @param d Number of bytes to delay by; +ve moves data later.
30  */
31 DelayLine::DelayLine (int d)
32         : _delay (d)
33         , _buffer (0)
34         , _negative_delay_remaining (0)
35 {
36         if (d > 0) {
37                 /* We need a buffer to keep some data in */
38                 _buffer = new uint8_t[d];
39                 memset (_buffer, 0, d);
40         } else if (d < 0) {
41                 /* We can do -ve delays just by chopping off
42                    the start, so no buffer needed.
43                 */
44                 _negative_delay_remaining = -d;
45         }
46 }
47
48 DelayLine::~DelayLine ()
49 {
50         delete[] _buffer;
51 }
52
53 int
54 DelayLine::feed (uint8_t* data, int size)
55 {
56         int available = size;
57
58         if (_delay > 0) {
59                 
60                 /* Copy the input data */
61                 uint8_t input[size];
62                 memcpy (input, data, size);
63
64                 int to_do = size;
65
66                 /* Write some of our buffer to the output */
67                 int const from_buffer = min (to_do, _delay);
68                 memcpy (data, _buffer, from_buffer);
69                 to_do -= from_buffer;
70
71                 /* Write some of the input to the output */
72                 int const from_input = min (to_do, size);
73                 memcpy (data + from_buffer, input, from_input);
74
75                 int const left_in_buffer = _delay - from_buffer;
76                 
77                 /* Shuffle our buffer down */
78                 memmove (_buffer, _buffer + from_buffer, left_in_buffer);
79
80                 /* Copy remaining input data to our buffer */
81                 memcpy (_buffer + left_in_buffer, input + from_input, size - from_input);
82
83         } else if (_delay < 0) {
84
85                 /* Chop the initial data off until _negative_delay_remaining
86                    is zero, then just pass data.
87                 */
88
89                 int const to_do = min (size, _negative_delay_remaining);
90                 available = size - to_do;
91                 memmove (data, data + to_do, available);
92                 _negative_delay_remaining -= to_do;
93
94         }
95
96         return available;
97 }
98
99 /** With -ve delays, the DelayLine will have data to give after
100  *  all input data has been passed to ::feed().
101  *  Call this method after passing all input data.
102  *
103  *  @param buffer Pointer to buffer of _delay bytes in length,
104  *  which will be filled with remaining data.
105  */
106 void
107 DelayLine::get_remaining (uint8_t* buffer)
108 {
109         memset (buffer, 0, -_delay);
110 }