Skip to content

Instantly share code, notes, and snippets.

@wolfhechel
Created April 29, 2015 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfhechel/860e557ef26ef46b7c0a to your computer and use it in GitHub Desktop.
Save wolfhechel/860e557ef26ef46b7c0a to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <archive.h>
#include <archive_entry.h>
#include <libavformat/avformat.h>
int read_archive_packet(void * opaque, uint8_t *buf, int buf_size) {
int data_read = (int)archive_read_data(opaque, buf, (size_t)buf_size);
fprintf(stderr, "buf_size = %d, read %d\n", buf_size, data_read);
if (data_read == 0)
data_read = AVERROR_EOF;
return data_read;
}
int64_t seek_archive_packet(void * opaque, int64_t offset, int whence) {
fprintf(stderr, "test 2\n");
struct archive * archive = opaque;
return 0;
}
void read_av_format(struct archive * archive, struct archive_entry * entry) {
/* Setup buffers */
const int buf_size = 32 * 1024;
/* Create our AVIOContext stream */
AVIOContext * av_io_context = avio_alloc_context(
malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE),
buf_size,
0,
archive,
read_archive_packet,
0,
seek_archive_packet
);
/* Allocate our format context */
AVFormatContext* pCtx = avformat_alloc_context();
pCtx->pb = av_io_context;
pCtx->flags = AVFMT_FLAG_CUSTOM_IO;
/* Now we need to probe for input format */
AVInputFormat * avFormat;
if (av_probe_input_buffer(av_io_context, &avFormat, archive_entry_pathname(entry), NULL, 0, buf_size) != 0) {
goto read_av_format_free;
}
pCtx->iformat = avFormat;
if (avformat_open_input(&pCtx, "", avFormat, 0) != 0)
goto read_av_format_free;
avformat_find_stream_info(pCtx, 0);
read_av_format_free:
free(pCtx->pb->buffer);
avformat_close_input(&pCtx);
}
void read_archive(struct archive * open_archive) {
struct archive_entry *entry;
while (archive_read_next_header(open_archive, &entry) == ARCHIVE_OK) {
printf("file = %s\n", archive_entry_pathname(entry));
read_av_format(open_archive, entry);
}
}
int main(int argc, char *argv[]) {
const char * path = NULL;
struct archive * archive = NULL;
for (int a = 1; a < argc; a++) {
path = argv[a];
if (access(path, R_OK) < 0) {
fprintf(stderr, "File %s does not exist\n", path);
break;
}
archive = archive_read_new();
if (archive != NULL) {
if ((archive_read_support_filter_all(archive) != ARCHIVE_OK) ||
(archive_read_support_format_all(archive) != ARCHIVE_OK)) {
archive_read_free(archive);
fprintf(stderr, "Failed to setup libarchive for reading\n");
archive = NULL;
}
}
if (archive != NULL) {
if (archive_read_open_filename(archive, path, 128) == ARCHIVE_OK) {
read_archive(archive);
}
archive_read_free(archive);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment