Skip to content

Commit 02e6457

Browse files
committed
ffmpeg dry out a bit, get rid of warnings
1 parent 5900ab3 commit 02e6457

File tree

3 files changed

+105
-141
lines changed

3 files changed

+105
-141
lines changed

ffmpeg/common.h

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#ifndef COMMON_H
2+
#define COMMON_H
3+
4+
#include <libavcodec/avcodec.h>
5+
#include <libavutil/imgutils.h>
6+
#include <libavutil/opt.h>
7+
#include <libswscale/swscale.h>
8+
9+
/*
10+
Generate 2 different images with four colored rectangles, each 25 frames long:
11+
12+
Image 1:
13+
14+
black | red
15+
------+-----
16+
green | blue
17+
18+
Image 2:
19+
20+
yellow | red
21+
-------+-----
22+
green | white
23+
*/
24+
void common_generate_rgb(int width, int height, int pts, uint8_t **rgbp) {
25+
int x, y, cur;
26+
uint8_t *rgb = *rgbp;
27+
rgb = realloc(rgb, 3 * sizeof(uint8_t) * height * width);
28+
for (y = 0; y < height; y++) {
29+
for (x = 0; x < width; x++) {
30+
cur = 3 * (y * width + x);
31+
rgb[cur + 0] = 0;
32+
rgb[cur + 1] = 0;
33+
rgb[cur + 2] = 0;
34+
if ((pts / 25) % 2 == 0) {
35+
if (y < height / 2) {
36+
if (x < width / 2) {
37+
/* Black. */
38+
} else {
39+
rgb[cur + 0] = 255;
40+
}
41+
} else {
42+
if (x < width / 2) {
43+
rgb[cur + 1] = 255;
44+
} else {
45+
rgb[cur + 2] = 255;
46+
}
47+
}
48+
} else {
49+
if (y < height / 2) {
50+
rgb[cur + 0] = 255;
51+
if (x < width / 2) {
52+
rgb[cur + 1] = 255;
53+
} else {
54+
rgb[cur + 2] = 255;
55+
}
56+
} else {
57+
if (x < width / 2) {
58+
rgb[cur + 1] = 255;
59+
rgb[cur + 2] = 255;
60+
} else {
61+
rgb[cur + 0] = 255;
62+
rgb[cur + 1] = 255;
63+
rgb[cur + 2] = 255;
64+
}
65+
}
66+
}
67+
}
68+
}
69+
*rgbp = rgb;
70+
}
71+
72+
#endif

ffmpeg/encode.c

+14-74
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ but modified to:
2121
- take rgb input instead of YUV
2222
*/
2323

24-
#include <libavcodec/avcodec.h>
25-
#include <libavutil/imgutils.h>
26-
#include <libavutil/opt.h>
27-
#include <libswscale/swscale.h>
24+
#include "common.h"
2825

2926
static AVCodecContext *c = NULL;
3027
static AVFrame *frame;
@@ -46,69 +43,6 @@ static void ffmpeg_encoder_set_frame_yuv_from_rgb(uint8_t *rgb) {
4643
c->height, frame->data, frame->linesize);
4744
}
4845

49-
/*
50-
Generate 2 different images with four colored rectangles, each 25 frames long:
51-
52-
Image 1:
53-
54-
black | red
55-
------+-----
56-
green | blue
57-
58-
Image 2:
59-
60-
yellow | red
61-
-------+-----
62-
green | white
63-
*/
64-
void generate_rgb(int width, int height, int pts, uint8_t **rgbp) {
65-
int x, y, cur;
66-
uint8_t *rgb = *rgbp;
67-
rgb = realloc(rgb, 3 * sizeof(uint8_t) * height * width);
68-
for (y = 0; y < height; y++) {
69-
for (x = 0; x < width; x++) {
70-
cur = 3 * (y * width + x);
71-
rgb[cur + 0] = 0;
72-
rgb[cur + 1] = 0;
73-
rgb[cur + 2] = 0;
74-
if ((frame->pts / 25) % 2 == 0) {
75-
if (y < height / 2) {
76-
if (x < width / 2) {
77-
/* Black. */
78-
} else {
79-
rgb[cur + 0] = 255;
80-
}
81-
} else {
82-
if (x < width / 2) {
83-
rgb[cur + 1] = 255;
84-
} else {
85-
rgb[cur + 2] = 255;
86-
}
87-
}
88-
} else {
89-
if (y < height / 2) {
90-
rgb[cur + 0] = 255;
91-
if (x < width / 2) {
92-
rgb[cur + 1] = 255;
93-
} else {
94-
rgb[cur + 2] = 255;
95-
}
96-
} else {
97-
if (x < width / 2) {
98-
rgb[cur + 1] = 255;
99-
rgb[cur + 2] = 255;
100-
} else {
101-
rgb[cur + 0] = 255;
102-
rgb[cur + 1] = 255;
103-
rgb[cur + 2] = 255;
104-
}
105-
}
106-
}
107-
}
108-
}
109-
*rgbp = rgb;
110-
}
111-
11246
/* Allocate resources and write header data to the output file. */
11347
void ffmpeg_encoder_start(const char *filename, int codec_id, int fps, int width, int height) {
11448
AVCodec *codec;
@@ -208,24 +142,30 @@ void ffmpeg_encoder_encode_frame(uint8_t *rgb) {
208142
}
209143

210144
/* Represents the main loop of an application which generates one frame per loop. */
211-
static void encode_example(const char *filename, int codec_id) {
145+
static void encode_example(const char *filename, int codec_id, int width, int height) {
212146
int pts;
213-
int width = 320;
214-
int height = 240;
215147
uint8_t *rgb = NULL;
216148
ffmpeg_encoder_start(filename, codec_id, 25, width, height);
217149
for (pts = 0; pts < 100; pts++) {
218150
frame->pts = pts;
219-
generate_rgb(width, height, pts, &rgb);
151+
common_generate_rgb(width, height, pts, &rgb);
220152
ffmpeg_encoder_encode_frame(rgb);
221153
}
222154
ffmpeg_encoder_finish();
223155
free(rgb);
224156
}
225157

226-
int main(void) {
227-
encode_example("tmp.h264", AV_CODEC_ID_H264);
228-
encode_example("tmp.mpg", AV_CODEC_ID_MPEG1VIDEO);
158+
int main(int argc, char **argv) {
159+
int width = 320;
160+
int height = 240;
161+
if (argc > 1) {
162+
width = strtol(argv[1], NULL, 10);
163+
if (argc > 2) {
164+
height = strtol(argv[2], NULL, 10);
165+
}
166+
}
167+
encode_example("tmp.h264", AV_CODEC_ID_H264, width, height);
168+
encode_example("tmp.mpg", AV_CODEC_ID_MPEG1VIDEO, width, height);
229169
/* TODO: is this encoded correctly? Possible to view it without container? */
230170
/*encode_example("tmp.vp8", AV_CODEC_ID_VP8);*/
231171
return 0;

ffmpeg/resize.c

+19-67
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
/*
2-
http://stackoverflow.com/questions/12831761/how-to-resize-a-picture-using-ffmpegs-sws-scale
3-
4-
Upstream: encode.c
2+
http://stackoverflow.com/questions/12831761/how-to-resize-a-picture-using-ffmpegs-sws-scale/36487785#36487785
53
*/
64

7-
#include <libavcodec/avcodec.h>
8-
#include <libavutil/imgutils.h>
9-
#include <libavutil/opt.h>
10-
#include <libswscale/swscale.h>
5+
#include "common.h"
116

127
static AVCodecContext *c = NULL;
138
static AVFrame *frame;
@@ -35,7 +30,7 @@ static void ffmpeg_encoder_init_frame(AVFrame **framep, int width, int height) {
3530
*framep = frame;
3631
}
3732

38-
static void ffmpeg_encoder_scale(uint8_t *rgb) {
33+
static void ffmpeg_encoder_scale(void) {
3934
sws_context = sws_getCachedContext(sws_context,
4035
frame->width, frame->height, AV_PIX_FMT_YUV420P,
4136
frame2->width, frame2->height, AV_PIX_FMT_YUV420P,
@@ -54,57 +49,8 @@ static void ffmpeg_encoder_set_frame_yuv_from_rgb(uint8_t *rgb) {
5449
frame->height, frame->data, frame->linesize);
5550
}
5651

57-
void generate_rgb(int width, int height, int pts, uint8_t **rgbp) {
58-
int x, y, cur;
59-
uint8_t *rgb = *rgbp;
60-
rgb = realloc(rgb, 3 * sizeof(uint8_t) * height * width);
61-
for (y = 0; y < height; y++) {
62-
for (x = 0; x < width; x++) {
63-
cur = 3 * (y * width + x);
64-
rgb[cur + 0] = 0;
65-
rgb[cur + 1] = 0;
66-
rgb[cur + 2] = 0;
67-
if ((frame->pts / 25) % 2 == 0) {
68-
if (y < height / 2) {
69-
if (x < width / 2) {
70-
/* Black. */
71-
} else {
72-
rgb[cur + 0] = 255;
73-
}
74-
} else {
75-
if (x < width / 2) {
76-
rgb[cur + 1] = 255;
77-
} else {
78-
rgb[cur + 2] = 255;
79-
}
80-
}
81-
} else {
82-
if (y < height / 2) {
83-
rgb[cur + 0] = 255;
84-
if (x < width / 2) {
85-
rgb[cur + 1] = 255;
86-
} else {
87-
rgb[cur + 2] = 255;
88-
}
89-
} else {
90-
if (x < width / 2) {
91-
rgb[cur + 1] = 255;
92-
rgb[cur + 2] = 255;
93-
} else {
94-
rgb[cur + 0] = 255;
95-
rgb[cur + 1] = 255;
96-
rgb[cur + 2] = 255;
97-
}
98-
}
99-
}
100-
}
101-
}
102-
*rgbp = rgb;
103-
}
104-
10552
void ffmpeg_encoder_start(const char *filename, int codec_id, int fps, int width, int height, float factor) {
10653
AVCodec *codec;
107-
int ret;
10854
int width2 = width * factor;
10955
int height2 = height * factor;
11056
avcodec_register_all();
@@ -169,7 +115,7 @@ void ffmpeg_encoder_finish(void) {
169115
void ffmpeg_encoder_encode_frame(uint8_t *rgb) {
170116
int ret, got_output;
171117
ffmpeg_encoder_set_frame_yuv_from_rgb(rgb);
172-
ffmpeg_encoder_scale(rgb);
118+
ffmpeg_encoder_scale();
173119
frame2->pts = frame->pts;
174120
av_init_packet(&pkt);
175121
pkt.data = NULL;
@@ -185,26 +131,32 @@ void ffmpeg_encoder_encode_frame(uint8_t *rgb) {
185131
}
186132
}
187133

188-
static void encode_example(float factor) {
134+
static void encode_example(int width, int height, float factor) {
189135
char filename[255];
190136
int pts;
191-
int width = 320;
192-
int height = 240;
193137
uint8_t *rgb = NULL;
194-
snprintf(filename, 255, "tmp." __FILE__ ".%.2f.h264", factor);
138+
snprintf(filename, 255, "tmp." __FILE__ "_%.2f.h264", factor);
195139
ffmpeg_encoder_start(filename, AV_CODEC_ID_H264, 25, width, height, factor);
196140
for (pts = 0; pts < 100; pts++) {
197141
frame->pts = pts;
198-
generate_rgb(width, height, pts, &rgb);
142+
common_generate_rgb(width, height, pts, &rgb);
199143
ffmpeg_encoder_encode_frame(rgb);
200144
}
201145
ffmpeg_encoder_finish();
202146
free(rgb);
203147
}
204148

205-
int main(void) {
206-
encode_example(0.5);
207-
encode_example(1.0);
208-
encode_example(2.0);
149+
int main(int argc, char **argv) {
150+
int width = 320;
151+
int height = 240;
152+
if (argc > 1) {
153+
width = strtol(argv[1], NULL, 10);
154+
if (argc > 2) {
155+
height = strtol(argv[2], NULL, 10);
156+
}
157+
}
158+
encode_example(width, height, 0.5);
159+
encode_example(width, height, 1.0);
160+
encode_example(width, height, 2.0);
209161
return EXIT_SUCCESS;
210162
}

0 commit comments

Comments
 (0)