summaryrefslogtreecommitdiff
path: root/src/input_mp3.h
blob: ad802a69c8083f0616a2fa97d96b15c39414a5e3 (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
#ifndef INPUT_MP3_H
#define INPUT_MP3_H

#include "mpaudec/mpaudec.h"
#include "audiere.h"
#include "basic_source.h"
#include "types.h"
#include "utility.h"

namespace audiere {

  class MP3InputStream : public BasicSource {
  public:
    MP3InputStream();
    ~MP3InputStream();

    bool initialize(FilePtr file);

    void ADR_CALL getFormat(
      int& channel_count,
      int& sample_rate,
      SampleFormat& sample_format);
    int doRead(int frame_count, void* samples);
    void ADR_CALL reset();

    bool ADR_CALL isSeekable();
    int  ADR_CALL getLength();
    void ADR_CALL setPosition(int position);
    int  ADR_CALL getPosition();


  private:
    void readID3v1Tags();
    void readID3v2Tags();
    void ID3v2Parse(u8* buf, int len, u8 version, u8 flags);
    bool ID3v2Match(u8* buf);
    bool decodeFrame();

    FilePtr m_file;
    bool m_eof;

    // from format chunk
    int m_channel_count;
    int m_sample_rate;
    SampleFormat m_sample_format;

    MPAuDecContext* m_context;

    QueueBuffer m_buffer;

    enum { INPUT_BUFFER_SIZE = 4096 };
    u8 m_input_buffer[INPUT_BUFFER_SIZE];
    int m_input_position;
    int m_input_length;
    u8* m_decode_buffer;
    bool m_first_frame;

    bool m_seekable;
    int m_length;
    int m_position;
    std::vector<int> m_frame_sizes;
    std::vector<int> m_frame_offsets;
  };

}

#endif