aboutsummaryrefslogtreecommitdiff
path: root/src/vhf_deframe_c2.c
blob: 754309834cc850fe108666ca4da2faddd4f6b7ff (plain)
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

/*---------------------------------------------------------------------------*\

  FILE........: vhf_deframe_c2.c
  AUTHOR......: Brady O'Brien
  DATE CREATED: 8 March 2016

  C tool to extract codec2 data from freedv VHF 2400A/B/whatever frames


\*---------------------------------------------------------------------------*/

/*
  Copyright (C) 2016 David Rowe

  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1, as
  published by the Free Software Foundation.  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 Lesser General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "freedv_vhf_framing.h"

int main(int argc, char *argv[]) {
  struct freedv_vhf_deframer *deframer;
  FILE *fin, *fout;
  uint8_t *bitbuf;
  uint8_t *c2buf;
  uint8_t zbuf[] = {0, 0, 0, 0, 0, 0, 0, 0};
  int frame_fmt = 0;
  int fsize, c2size;

  if (argc < 3) {
    fprintf(stderr, "usage: %s (A|B) InputOneBitPerCharFile OutputC2File\n",
            argv[0]);
    exit(1);
  }

  if (strcmp(argv[1], "A") == 0) {
    frame_fmt = FREEDV_VHF_FRAME_A;
  } else if (strcmp(argv[1], "B") == 0) {
    frame_fmt = FREEDV_HF_FRAME_B;
  } else {
    fprintf(stderr, "usage: %s (A|B) InputOneBitPerCharFile OutputC2File\n",
            argv[0]);
    exit(1);
  }

  /* Open files */
  if (strcmp(argv[2], "-") == 0) {
    fin = stdin;
  } else {
    fin = fopen(argv[2], "r");
  }

  if (strcmp(argv[3], "-") == 0) {
    fout = stdout;
  } else {
    fout = fopen(argv[3], "w");
  }

  /* Set up deframer */
  deframer = fvhff_create_deframer(frame_fmt, 0);

  if (fin == NULL || fout == NULL || deframer == NULL) {
    fprintf(stderr, "Couldn't open test vector files\n");
    goto cleanup;
  }

  c2size = fvhff_get_codec2_size(deframer);
  fsize = fvhff_get_frame_size(deframer);

  /* allocate buffers for processing */
  bitbuf = (uint8_t *)malloc(sizeof(uint8_t) * fsize);
  c2buf = (uint8_t *)malloc(sizeof(uint8_t) * c2size);

  /* Deframe! */
  while (fread(bitbuf, sizeof(uint8_t), fsize, fin) == fsize) {
    if (fvhff_deframe_bits(deframer, c2buf, NULL, NULL, bitbuf))
      fwrite(c2buf, sizeof(uint8_t), c2size, fout);
    else
      fwrite(zbuf, sizeof(uint8_t), c2size, fout);

    if (fout == stdin) {
      fflush(fout);
    }
  }
  fprintf(stderr, "BER Estimate: %f total_uw_err: %d \n",
          ((float)deframer->total_uw_err) / ((float)deframer->total_uw_bits),
          deframer->total_uw_err);

  free(bitbuf);
  free(c2buf);

cleanup:
  fclose(fin);
  fclose(fout);
  fvhff_destroy_deframer(deframer);
  exit(0);
}