Skip to content

Commit

Permalink
add avs file reader
Browse files Browse the repository at this point in the history
  • Loading branch information
ireader committed Apr 22, 2023
1 parent 1aae297 commit 8ef0687
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 0 deletions.
42 changes: 42 additions & 0 deletions libmov/test/mov-avs-buffer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#if defined(__AVS__)

#include "mov-buffer.h"
#include "mov-avs-buffer.h"
#include "avs-file.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

static int mov_avs_read(void* fp, void* data, uint64_t bytes)
{
return avs_file_read((struct avs_file_t*)fp, data, bytes);
}

static int mov_avs_write(void* fp, const void* data, uint64_t bytes)
{
return -1;
}

static int mov_avs_seek(void* fp, int64_t offset)
{
return avs_file_seek((struct avs_file_t*)fp, offset, offset >= 0 ? AVS_SEEK_SET : AVS_SEEK_END);
}

static int64_t mov_avs_tell(void* fp)
{
return avs_file_tell((struct avs_file_t*)fp);
}

const struct mov_buffer_t* mov_avs_buffer(void)
{
static struct mov_buffer_t s_io = {
mov_avs_read,
mov_avs_write,
mov_avs_seek,
mov_avs_tell,
};
return &s_io;
}

#endif /* __AVS__*/
20 changes: 20 additions & 0 deletions libmov/test/mov-avs-buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef _mov_file_buffer_h_
#define _mov_file_buffer_h_

#if defined(__AVS__)

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

#ifdef __cplusplus
extern "C" {
#endif

const struct mov_buffer_t* mov_avs_buffer(void);

#ifdef __cplusplus
}
#endif

#endif /* __AVS__ */
#endif /* !_mov_file_buffer_h_ */
125 changes: 125 additions & 0 deletions librtsp/test/media/avs-file-reader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#if defined(__AVS__)

#include "avs-file-reader.h"
#include "mov-format.h"
#include "avcodecid.h"
#include "rtsp-payloads.h"

extern "C" const struct mov_buffer_t* mov_avs_buffer(void);

AVSFileReader::AVSFileReader(struct avs_storage_t* avs, const char* channel, const char* file)
:m_fp(NULL), m_pos(0), m_reader(NULL)
{
memset(&m_utils, 0, sizeof(m_utils));

uint64_t total;
int64_t from, to;
if (3 == sscanf(file, "v1-%*d-%*d-%*d-%" PRId64 "-%" PRId64 "-%" PRIu64, &from, &to, &total))
{
m_fp = avs_file_create(avs, channel, from, to);
if (m_fp)
{
m_reader = mov_reader_create(mov_avs_buffer(), m_fp);
GetInfo();
}
}
}

AVSFileReader::~AVSFileReader()
{
avpktutil_destroy(&m_utils);

if (m_reader)
{
mov_reader_destroy(m_reader);
m_reader = NULL;
}

if (m_fp)
{
avs_file_destroy(m_fp);
m_fp = NULL;
}
}

int AVSFileReader::GetInfo()
{
if (!m_reader)
return -1;
struct mov_reader_trackinfo_t info = { OnVideoInfo, OnAudioInfo, OnSubtitleInfo};
return mov_reader_getinfo(m_reader, &info, this);
}

int AVSFileReader::GetInfo(struct mov_reader_trackinfo_t* info, void* param)
{
if (!m_reader)
return -1;
return mov_reader_getinfo(m_reader, info, param);
}

int AVSFileReader::Read(struct avpacket_t** pkt)
{
m_pkt = pkt;
int r = mov_reader_read(m_reader, m_packet, sizeof(m_packet), MP4OnRead, this);
if (r < 0 || NULL == *pkt)
return *pkt ? r : -1; //ENOMEM

if(*pkt)
m_pos = (*pkt)->dts; // update offset
return r;
}

int AVSFileReader::Seek(uint64_t* pos, int /*strategy*/)
{
int r = mov_reader_seek(m_reader, (int64_t*)pos);
if (0 == r)
m_pos = (int64_t)*pos;
return r;
}

uint64_t AVSFileReader::GetPosotion()
{
return m_pos;
}

uint64_t AVSFileReader::GetDuration()
{
if (!m_reader)
return 0;
return mov_reader_getduration(m_reader);
}

void AVSFileReader::OnVideoInfo(void* param, uint32_t track, uint8_t object, int width, int height, const void* extra, size_t bytes)
{
AVSFileReader* self = (AVSFileReader*)param;
int r = avpayload_find_by_mov(object);
if (r == -1)
return;
avpktutil_addvideo(&self->m_utils, track, s_payloads[r].codecid, width, height, extra, bytes);
}

void AVSFileReader::OnAudioInfo(void* param, uint32_t track, uint8_t object, int channel_count, int bit_per_sample, int sample_rate, const void* extra, size_t bytes)
{
AVSFileReader* self = (AVSFileReader*)param;
int r = avpayload_find_by_mov(object);
if (r == -1)
return;
avpktutil_addaudio(&self->m_utils, track, s_payloads[r].codecid, channel_count, bit_per_sample, sample_rate, extra, bytes);
}

void AVSFileReader::OnSubtitleInfo(void* param, uint32_t track, uint8_t object, const void* extra, size_t bytes)
{
AVSFileReader* self = (AVSFileReader*)param;
int r = avpayload_find_by_mov(object);
if (r == -1)
return;
avpktutil_addsubtitle(&self->m_utils, track, s_payloads[r].codecid, extra, bytes);
}

void AVSFileReader::MP4OnRead(void* param, uint32_t track, const void* buffer, size_t bytes, int64_t pts, int64_t dts, int flags)
{
AVSFileReader* self = (AVSFileReader*)param;
avpktutil_input(&self->m_utils, self->m_utils.streams[track-1], buffer, bytes, pts, dts, flags, self->m_pkt);
}

#endif /* __AVS__ */
45 changes: 45 additions & 0 deletions librtsp/test/media/avs-file-reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef _avs_file_reader_h_
#define _avs_file_reader_h_

#if defined(__AVS__)
#include "vod-file-source.h"
#include "mov-reader.h"
#include "avpktutil.h"
#include "avs-file.h"
#include <string>
#include <stdio.h>
#include <stdint.h>

class AVSFileReader : public VodFileSource::IFileReader
{
public:
AVSFileReader(struct avs_storage_t* avs, const char* channel, const char* file);
virtual ~AVSFileReader();

public:
virtual int Read(struct avpacket_t** pkt);
virtual int Seek(uint64_t* pos, int strategy);
virtual uint64_t GetPosotion();
virtual uint64_t GetDuration();

public:
int GetInfo();
int GetInfo(struct mov_reader_trackinfo_t* info, void* param);

private:
static void OnVideoInfo(void* param, uint32_t track, uint8_t object, int width, int height, const void* extra, size_t bytes);
static void OnAudioInfo(void* param, uint32_t track, uint8_t object, int channel_count, int bit_per_sample, int sample_rate, const void* extra, size_t bytes);
static void OnSubtitleInfo(void* param, uint32_t track, uint8_t object, const void* extra, size_t bytes);
static void MP4OnRead(void* param, uint32_t track, const void* buffer, size_t bytes, int64_t pts, int64_t dts, int flags);

private:
avs_file_t* m_fp;
int64_t m_pos;
mov_reader_t* m_reader;
struct avpacket_t** m_pkt;
struct avpktutil_t m_utils;
uint8_t m_packet[2 * 1024 * 1024];
};

#endif /* __AVS__ */
#endif /* !_avs_file_reader_h_ */

0 comments on commit 8ef0687

Please sign in to comment.