/
main.c
102 lines (83 loc) · 2.46 KB
/
main.c
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
/*
* Copyright (c) 2016 Dmitry Osipenko <digetx@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 General Public License along
* with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "decoder.h"
#include "syntax_parse.h"
static void save_decoded_frame(decoder_context *decoder, frame_data *frame)
{
FILE *fp = decoder->opaque;
long foff = ftell(fp);
fwrite(p2v(frame->Y_paddr), 1, frame_luma_size(decoder), fp);
fwrite(p2v(frame->U_paddr), 1, frame_chroma_size(decoder), fp);
fwrite(p2v(frame->V_paddr), 1, frame_chroma_size(decoder), fp);
if (ferror(fp) != 0) {
perror("Error writing to output file");
abort();
}
printf("Saved frame %d file offset 0x%lX\n",
decoder->frames_decoded - 1, foff);
}
int main(int argc, char **argv)
{
struct stat sb;
decoder_context decoder;
const char *in_file_path = NULL;
const char *out_file_path = NULL;
FILE *fp_out;
void *data_ptr;
int fd;
int c;
while ((c = getopt(argc, argv, "i:o:")) != -1) {
switch (c) {
case 'i':
in_file_path = optarg;
break;
case 'o':
out_file_path = optarg;
break;
default:
break;
}
}
if (in_file_path == NULL || out_file_path == NULL) {
fprintf(stderr, "-i h264 input file path\n");
fprintf(stderr, "-o decoded i420 frames output file path\n");
exit(EXIT_FAILURE);
}
fd = open(in_file_path, O_RDONLY);
assert(fd != -1);
assert(fstat(fd, &sb) != -1);
fp_out = fopen(out_file_path, "w+");
assert(fp_out != NULL);
data_ptr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
assert(data_ptr != MAP_FAILED);
assert(madvise(data_ptr, sb.st_size, MADV_SEQUENTIAL) == 0);
decoder_init(&decoder, data_ptr, sb.st_size);
if (out_file_path) {
decoder_set_notify(&decoder, save_decoded_frame, fp_out);
}
if (!parse_mp4(&decoder)) {
parse_annex_b(&decoder);
}
return 0;
}