MythTV  master
flacencoder.cpp
Go to the documentation of this file.
1 #include <unistd.h>
2 
3 #include <cstdlib>
4 
5 #include <iostream>
6 using namespace std;
7 
8 #include <QString>
9 
10 // libmythmetadata
11 #include <musicmetadata.h>
12 #include <metaioflacvorbis.h>
13 
14 #include "flacencoder.h"
15 
16 #include <FLAC/export.h>
17 #if !defined(NEWFLAC)
18  /* FLAC 1.0.4 to 1.1.2 */
19  #include <FLAC/file_encoder.h>
20 #else
21  /* FLAC 1.1.3 and up */
22  #include <FLAC/stream_encoder.h>
23 #endif
24 
25 #include <FLAC/assert.h>
26 #include <mythcontext.h>
27 
28 FlacEncoder::FlacEncoder(const QString &outfile, int qualitylevel,
29  MusicMetadata *metadata)
30  : Encoder(outfile, qualitylevel, metadata)
31 {
32  bool streamable_subset = true;
33  bool do_mid_side = true;
34  bool loose_mid_side = false;
35  int bits_per_sample = 16;
36  int sample_rate = 44100;
37  int blocksize = 4608;
38  int max_lpc_order = 8;
39  int qlp_coeff_precision = 0;
40  bool qlp_coeff_prec_search = false;
41  bool do_escape_coding = false;
42  bool do_exhaustive_model_search = false;
43  int min_residual_partition_order = 3;
44  int max_residual_partition_order = 3;
45  int rice_parameter_search_dist = 0;
46 
48  encoder_setup(m_encoder, streamable_subset,
49  do_mid_side, loose_mid_side,
50  NUM_CHANNELS, bits_per_sample,
51  sample_rate, blocksize,
52  max_lpc_order, qlp_coeff_precision,
53  qlp_coeff_prec_search, do_escape_coding,
54  do_exhaustive_model_search,
55  min_residual_partition_order,
56  max_residual_partition_order,
57  rice_parameter_search_dist);
58 
59  QByteArray ofile = outfile.toLocal8Bit();
60 #if !defined(NEWFLAC)
61  /* FLAC 1.0.4 to 1.1.2 */
62  FLAC__file_encoder_set_filename(m_encoder, ofile.constData());
63 
64  int ret = FLAC__file_encoder_init(m_encoder);
65  if (ret != FLAC__FILE_ENCODER_OK)
66 #else
67  /* FLAC 1.1.3 and up */
68  int ret = FLAC__stream_encoder_init_file(
69  m_encoder, ofile.constData(), nullptr, nullptr);
70  if (ret != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
71 #endif
72  {
73  LOG(VB_GENERAL, LOG_ERR,
74  QString("Error initializing FLAC encoder. Got return code: %1")
75  .arg(ret));
76  }
77 
78  for (auto & i : m_inputIn)
79  for (int & j : i)
80  j = 0;
81 
82  for (int i = 0; i < NUM_CHANNELS; i++)
83  m_input[i] = &(m_inputIn[i][0]);
84 }
85 
87 {
88  FlacEncoder::addSamples(nullptr, 0); // flush buffer
89 
90  if (m_encoder)
91  {
94  }
95 
96  if (m_metadata)
98 }
99 
100 int FlacEncoder::addSamples(int16_t *bytes, unsigned int length)
101 {
102  unsigned int index = 0;
103 
104  length /= sizeof(int16_t);
105 
106  do {
107  while (bytes && index < length && m_sampleIndex < MAX_SAMPLES)
108  {
109  m_input[0][m_sampleIndex] = (FLAC__int32)(bytes[index++]);
110  m_input[1][m_sampleIndex] = (FLAC__int32)(bytes[index++]);
111  m_sampleIndex += 1;
112  }
113 
114  if(m_sampleIndex == MAX_SAMPLES || (length == 0 && m_sampleIndex > 0) )
115  {
117  (const FLAC__int32 * const *) m_input,
118  m_sampleIndex))
119  {
120  LOG(VB_GENERAL, LOG_ERR,
121  QString("Failed to write flac data. Aborting."));
122  return EENCODEERROR;
123  }
124  m_sampleIndex = 0;
125  }
126  } while (index < length);
127 
128  return 0;
129 }
130 
NUM_CHANNELS
#define NUM_CHANNELS
Definition: flacencoder.h:77
MAX_SAMPLES
#define MAX_SAMPLES
Definition: flacencoder.h:76
FlacEncoder::m_inputIn
FLAC__int32 m_inputIn[NUM_CHANNELS][MAX_SAMPLES]
Definition: flacencoder.h:92
encoder_process
#define encoder_process(enc, data, index)
Definition: flacencoder.h:37
FlacEncoder::~FlacEncoder
~FlacEncoder() override
Definition: flacencoder.cpp:86
FlacEncoder::addSamples
int addSamples(int16_t *bytes, unsigned int len) override
Definition: flacencoder.cpp:100
flacencoder.h
arg
arg(title).arg(filename).arg(doDelete))
Encoder
Definition: mythplugins/mythmusic/mythmusic/encoder.h:14
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MusicMetadata
Definition: musicmetadata.h:81
metaioflacvorbis.h
EENCODEERROR
#define EENCODEERROR
Definition: mythplugins/mythmusic/mythmusic/encoder.h:7
MetaIOFLACVorbis::write
bool write(const QString &filename, MusicMetadata *mdata) override
Writes all metadata back to a file.
Definition: metaioflacvorbis.cpp:40
FlacEncoder::m_input
FLAC__int32 * m_input[NUM_CHANNELS]
Definition: flacencoder.h:93
encoder_new
#define encoder_new()
Definition: flacencoder.h:10
encoder_delete
#define encoder_delete(enc)
Definition: flacencoder.h:36
FlacEncoder::m_encoder
FLAC_ENCODER * m_encoder
Definition: flacencoder.h:90
encoder_finish
#define encoder_finish(enc)
Definition: flacencoder.h:35
Encoder::m_metadata
MusicMetadata * m_metadata
Definition: mythplugins/mythmusic/mythmusic/encoder.h:26
musicbrainzngs.compat.bytes
bytes
Definition: compat.py:49
FlacEncoder::FlacEncoder
FlacEncoder(const QString &outfile, int qualitylevel, MusicMetadata *metadata)
Definition: flacencoder.cpp:28
MetaIOFLACVorbis
Read and write Vorbis (Xiph) tags in a FLAC file.
Definition: metaioflacvorbis.h:22
mythcontext.h
Encoder::m_outfile
const QString m_outfile
Definition: mythplugins/mythmusic/mythmusic/encoder.h:23
FlacEncoder::m_sampleIndex
unsigned int m_sampleIndex
Definition: flacencoder.h:91
encoder_setup
#define encoder_setup(enc, streamable_subset, do_mid_side_stereo, loose_mid_side_stereo, channels, bits_per_sample, sample_rate, blocksize, max_lpc_order, qlp_coeff_precision, do_qlp_coeff_prec_search, do_escape_coding, do_exhaustive_model_search, min_residual_partition_order, max_residual_partition_order, rice_parameter_search_dist)
Definition: flacencoder.h:11
musicmetadata.h