-
Notifications
You must be signed in to change notification settings - Fork 25
/
output.c
191 lines (171 loc) · 3.94 KB
/
output.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "picture_t.h"
#include "matroska_ebml.h"
#include "simplerecorder.h"
#define OUTPUT_FILENAME "./Record/20170518/11:12:35.mkv"
#define FPS 30
static int width, height;
mk_writer *writer;
static int cache_size=0;
static uint8_t *cache=NULL;
static int writing_frame = 0;
static uint8_t *findDelimiter(uint8_t **_p, uint8_t *end)
{
int cnt_0 = 0;
uint8_t *ret = 0, *p = *_p;
//printf("P %x,end %x \n",*p,end);
while(p < end){
if(!*p){
if(!ret)
ret=p;
cnt_0++;
}else{
if(*p==1 && cnt_0>=2){
(*_p)++;
return ret;
}
cnt_0 = 0;
ret = 0;
}
p++;
(*_p)++;
}
return NULL;
}
static int write_frame_data(uint8_t *ptr, int size)
{
if(size+4>cache_size){
cache_size=size+4;
cache = realloc(cache, cache_size);
if(!cache)
return 0;
}
*cache = (size>>24);
*(cache+1) = (size>>16)&255;
*(cache+2) = (size>>8)&255;
*(cache+3) = (size)&255;
memcpy(cache+4, ptr, size);
if(mk_add_frame_data(writer, cache, size+4) < 0)
return 0;
return 1;
}
int output_init(struct picture_t *info, const char *str)
{
if((writer=mk_create_writer(str)) == 0){
fprintf(stderr,"mk_create_writer failed:%s\n",str);
return 0;
}
width = info->width;
height = info->height;
return 1;
}
int output_write_headers(struct encoded_pic_t *headers)
{
uint8_t *avcC; // AVCDecoderConfigurationRecord
int avcC_len;
uint8_t *tmp, *last = NULL;
uint8_t *sps, *pps, *sei;
int sps_len=0, pps_len=0, sei_len=0;
int ret;
//printf("header_pic_length %d\n",(int)(headers->length));
//printf("header_pic_buffer @%x\n",(int)(headers->buffer));
uint64_t frame_duration = 1000000000ull/FPS;
//printf("tmp @%x\n",(int)(tmp));
for(tmp = headers->buffer;;){
//printf("tmp @%x\n",(int)(tmp));
uint8_t *st = findDelimiter(&tmp, headers->buffer + headers->length);
int len = st ? st-last : headers->buffer + headers->length - last;
//printf("tmp @%x\n",(int)(tmp));
if(last){
switch(*last&31){
case 6:
sei = last;
sei_len = len;
break;
case 7:
sps = last;
sps_len = len;
break;
case 8:
pps = last;
pps_len = len;
break;
}
}
last = tmp;
if(!st)
break;
}
//printf("sps @%x,%d,%x\n",(int)(sps),sps_len,(int)(*sps));
//printf("pps @%x,%d,%x\n",(int)(pps),pps_len,(int)(*pps));
//printf("sei @%x,%d,%x\n",(int)(sei),sei_len,(int)(*(sei)));
avcC_len = 5 + 1 + 2 + sps_len + 1 + 2 + pps_len;
avcC = malloc( avcC_len );
if( !avcC )
return 0;
avcC[0] = 1;
avcC[1] = sps[1];
avcC[2] = sps[2];
avcC[3] = sps[3];
avcC[4] = 0xff; // nalu size length is four bytes
avcC[5] = 0xe1; // one sps
avcC[6] = sps_len >> 8;
avcC[7] = sps_len;
memcpy( avcC+8, sps, sps_len );
avcC[8+sps_len] = 1; // one pps
avcC[9+sps_len] = pps_len >> 8;
avcC[10+sps_len] = pps_len;
memcpy( avcC+11+sps_len, pps, pps_len );
ret = mk_write_header( writer, "simplerecorder", "V_MPEG4/ISO/AVC",
avcC, avcC_len, frame_duration, 50000,
width, height, width, height, DS_PIXELS);
free( avcC );
if(ret < 0)
return 0;
if(sei_len){
if(mk_start_frame(writer) < 0)
return 0;
writing_frame = 1;
if(!write_frame_data(sei, sei_len))
return 0;
}
return 1;
}
int output_write_frame(struct encoded_pic_t *encoded)
{
uint8_t *ptr = encoded->buffer;
int length = encoded->length;
if(length == 0)
return 1;
while(length>0 && *ptr == 0){
ptr++;
length--;
}
if(!length || *ptr != 0x01){
fprintf(stderr, "invalid NAL delimiter\n");
return 0;
}
if(!writing_frame)
if(mk_start_frame(writer) < 0)
return 0;
if(!write_frame_data(ptr+1, length-1))
return 0;
if(mk_set_frame_flags(writer, encoded->timepoint*1000 /*us -> ns*/,
encoded->frame_type==FRAME_TYPE_I, encoded->frame_type==FRAME_TYPE_B)<0)
return 0;
writing_frame = 0;
return 1;
}
void output_close()
{
mk_close(writer, 0);
//free(cache);
}