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
|
#include "img_encoder.h"
#include <stdio.h>
#include "debug.h"
ImgEncoder::ImgEncoder()
{
av_register_all();
AVCodec *deccodec;
deccodec = avcodec_find_decoder(CODEC_ID_DVVIDEO);
if (!deccodec) {
fprintf(stderr, "codec not found\n");
exit(1);
}
dcc= avcodec_alloc_context(); ALLOC(dcc, "img_encoder, dcc");
if (avcodec_open(dcc, deccodec) < 0) {
fprintf(stderr, "could not open codec\n");
exit(1);
}
}
ImgEncoder::~ImgEncoder()
{
}
void ImgEncoder::encode(Frame *dvframe,
char *filename,
int quality)
{
int ret;
AVFrame *rawframe = avcodec_alloc_frame(); ALLOC(dcc, "img_encoder, rawframe");
uint8_t *ptr;
int got_picture = 1;
int len;
ptr = (uint8_t *)dvframe->data;
len = dvframe->size;
ret = avcodec_decode_video(dcc, rawframe, &got_picture, ptr, len);
if(!ret) {
printf("Decoder fuckup!\n");
return;
}
AVPicture pict;
avpicture_alloc(&pict,PIX_FMT_RGB24, 720, 576); ALLOC(dcc, "img_encoder, pict");
img_convert(&pict, PIX_FMT_RGB24, (AVPicture *)rawframe,
PIX_FMT_YUV420P, 720, 576);
printf("converted\n");
writeJPEGFile(filename, quality, (JSAMPLE*)(pict.data[0]), 720, 576);
printf("written\n");
avpicture_free(&pict); FREE(&pict);
av_free(rawframe); FREE(rawframe);
}
void ImgEncoder::writeJPEGFile(char *filename,
int quality,
JSAMPLE * image_buffer,
int image_width,
int image_height
)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE * outfile;
JSAMPROW row_pointer[1];
int row_stride;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
if ((outfile = fopen(filename, "wb")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
exit(1);
}
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = image_width;
cinfo.image_height = image_height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
row_stride = image_width * 3;
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
fclose(outfile);
jpeg_destroy_compress(&cinfo);
}
|