| 1 | #include <stdio.h>
|
|---|
| 2 | #include <stdint.h>
|
|---|
| 3 | #include <stddef.h>
|
|---|
| 4 | #include <string.h>
|
|---|
| 5 | #include <assert.h>
|
|---|
| 6 | #include <limits.h>
|
|---|
| 7 | #include <libavcodec/avcodec.h>
|
|---|
| 8 | #include <libavformat/avformat.h>
|
|---|
| 9 | #include <libavutil/opt.h>
|
|---|
| 10 | #include <libavutil/mem.h>
|
|---|
| 11 | #include <libavutil/avutil.h>
|
|---|
| 12 |
|
|---|
| 13 | enum
|
|---|
| 14 | {
|
|---|
| 15 | width = 352,
|
|---|
| 16 | height = 288,
|
|---|
| 17 | pts_increment = 50
|
|---|
| 18 | };
|
|---|
| 19 |
|
|---|
| 20 | AVCodecContext* create_encoder_context(int request_mb_info)
|
|---|
| 21 | {
|
|---|
| 22 | int res = 0;
|
|---|
| 23 | AVCodec* codec = NULL;
|
|---|
| 24 | AVCodecContext* av_ctx = avcodec_alloc_context3(NULL);
|
|---|
| 25 | assert(av_ctx != NULL);
|
|---|
| 26 |
|
|---|
| 27 | av_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
|
|---|
| 28 |
|
|---|
| 29 | av_ctx->codec_id = CODEC_ID_H263;
|
|---|
| 30 | av_ctx->rtp_payload_size = 1400;
|
|---|
| 31 | av_ctx->thread_count = 1;
|
|---|
| 32 |
|
|---|
| 33 | av_ctx->qcompress = 0.5;
|
|---|
| 34 | av_ctx->max_qdiff = 8;
|
|---|
| 35 | av_ctx->max_b_frames = 0;
|
|---|
| 36 |
|
|---|
| 37 | av_ctx->flags = 0;
|
|---|
| 38 | av_ctx->rc_max_rate = 0;
|
|---|
| 39 | av_ctx->rc_min_rate = 0;
|
|---|
| 40 | av_ctx->rc_buffer_size = 0;
|
|---|
| 41 | av_ctx->slice_count = 0;
|
|---|
| 42 | av_ctx->profile = FF_PROFILE_UNKNOWN;
|
|---|
| 43 |
|
|---|
| 44 | av_ctx->level = FF_LEVEL_UNKNOWN;
|
|---|
| 45 |
|
|---|
| 46 | av_ctx->me_method = ME_EPZS;
|
|---|
| 47 | av_ctx->me_cmp |= FF_CMP_CHROMA;
|
|---|
| 48 | av_ctx->me_subpel_quality = 0;
|
|---|
| 49 | av_ctx->me_range = 16;
|
|---|
| 50 | av_ctx->scenechange_threshold = 0;
|
|---|
| 51 | av_ctx->i_quant_factor = 0.9;
|
|---|
| 52 | av_ctx->b_frame_strategy = 1;
|
|---|
| 53 | av_ctx->refs = 1;
|
|---|
| 54 | av_ctx->trellis = 0;
|
|---|
| 55 | av_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
|
|---|
| 56 |
|
|---|
| 57 | av_ctx->width = width;
|
|---|
| 58 | av_ctx->height = height;
|
|---|
| 59 | av_ctx->pix_fmt = PIX_FMT_YUV420P;
|
|---|
| 60 | av_ctx->color_range = AVCOL_RANGE_JPEG;
|
|---|
| 61 |
|
|---|
| 62 | av_ctx->time_base.num = 1;
|
|---|
| 63 | av_ctx->time_base.den = 1000;
|
|---|
| 64 |
|
|---|
| 65 | av_ctx->gop_size = 50;
|
|---|
| 66 | av_ctx->keyint_min = 3;
|
|---|
| 67 | av_ctx->qmin = 2;
|
|---|
| 68 | av_ctx->qmax = 20;
|
|---|
| 69 | av_ctx->bit_rate = 800 * 1000;
|
|---|
| 70 | av_ctx->bit_rate_tolerance = av_ctx->bit_rate * 0.25f;
|
|---|
| 71 |
|
|---|
| 72 | codec = avcodec_find_encoder(av_ctx->codec_id);
|
|---|
| 73 | assert(codec != NULL);
|
|---|
| 74 | res = avcodec_open2(av_ctx, codec, NULL);
|
|---|
| 75 | assert(res == 0);
|
|---|
| 76 |
|
|---|
| 77 | if (request_mb_info)
|
|---|
| 78 | {
|
|---|
| 79 | res = av_opt_set_int(av_ctx, "mb_info", 1400, AV_OPT_SEARCH_CHILDREN);
|
|---|
| 80 | assert(res == 0);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | return av_ctx;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | void destroy_encoder_context(AVCodecContext* p)
|
|---|
| 87 | {
|
|---|
| 88 | if (p)
|
|---|
| 89 | {
|
|---|
| 90 | if (p->internal)
|
|---|
| 91 | avcodec_close(p);
|
|---|
| 92 | if (p->extradata)
|
|---|
| 93 | {
|
|---|
| 94 | av_freep(&p->extradata);
|
|---|
| 95 | p->extradata_size = 0;
|
|---|
| 96 | }
|
|---|
| 97 | av_free(p);
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | void init_frame(AVFrame* in_frame, size_t* raw_size)
|
|---|
| 102 | {
|
|---|
| 103 | uint8_t* data = NULL;
|
|---|
| 104 | size_t size = 0;
|
|---|
| 105 |
|
|---|
| 106 | memset(in_frame, 0, sizeof(*in_frame));
|
|---|
| 107 | avcodec_get_frame_defaults(in_frame);
|
|---|
| 108 |
|
|---|
| 109 | *raw_size = size = avpicture_get_size(PIX_FMT_YUV420P, width, height);
|
|---|
| 110 | data = av_malloc(size);
|
|---|
| 111 | assert(data != NULL);
|
|---|
| 112 | memset(data, 0, size);
|
|---|
| 113 |
|
|---|
| 114 | avpicture_fill((AVPicture*)in_frame, data, PIX_FMT_YUV420P, width, height);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | void generate_frame(AVFrame* in_frame, size_t raw_size, unsigned int pts)
|
|---|
| 118 | {
|
|---|
| 119 | unsigned int i = 0, j, n = (width - 20) / 2, threshold = (pts / pts_increment) % n;
|
|---|
| 120 | unsigned char y, cb, cr;
|
|---|
| 121 |
|
|---|
| 122 | for (i = 0; i < width / 2; ++i)
|
|---|
| 123 | {
|
|---|
| 124 | y = i < threshold ? 0 : 255;
|
|---|
| 125 | if (threshold & 8u)
|
|---|
| 126 | y = ~y;
|
|---|
| 127 | in_frame->data[0][i] = in_frame->data[0][width - i - 1] = y;
|
|---|
| 128 | j = i / 2;
|
|---|
| 129 | if (i < threshold)
|
|---|
| 130 | {
|
|---|
| 131 | cb = cr = 127;
|
|---|
| 132 | }
|
|---|
| 133 | else
|
|---|
| 134 | {
|
|---|
| 135 | cb = 127.0f * (float)(i - threshold) / (float)(width / 2 - threshold);
|
|---|
| 136 | cr = -cb;
|
|---|
| 137 | }
|
|---|
| 138 | in_frame->data[1][j] = in_frame->data[1][(width / 2) - j - 1] = cb;
|
|---|
| 139 | in_frame->data[2][j] = in_frame->data[2][(width / 2) - j - 1] = cr;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | for (i = 1; i < height; ++i)
|
|---|
| 143 | {
|
|---|
| 144 | memcpy(in_frame->data[0] + i * in_frame->linesize[0], in_frame->data[0], width);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | for (i = 1; i < height / 2; ++i)
|
|---|
| 148 | {
|
|---|
| 149 | memcpy(in_frame->data[1] + i * in_frame->linesize[1], in_frame->data[1], width / 2);
|
|---|
| 150 | memcpy(in_frame->data[2] + i * in_frame->linesize[2], in_frame->data[2], width / 2);
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | void init_packet(AVPacket* packet)
|
|---|
| 155 | {
|
|---|
| 156 | memset(packet, 0, sizeof(*packet));
|
|---|
| 157 | av_init_packet(packet);
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | AVFormatContext* create_format_context()
|
|---|
| 161 | {
|
|---|
| 162 | AVCodec* video_codec = NULL;
|
|---|
| 163 | AVCodecContext* av_video_codec_ctx = NULL;
|
|---|
| 164 | AVStream* av_stream = NULL;
|
|---|
| 165 | int res = 0;
|
|---|
| 166 | AVFormatContext* av_format_ctx = avformat_alloc_context();
|
|---|
| 167 | assert(av_format_ctx != NULL);
|
|---|
| 168 |
|
|---|
| 169 | av_format_ctx->oformat = av_guess_format("avi", "output.avi", NULL);
|
|---|
| 170 | assert(av_format_ctx->oformat != NULL);
|
|---|
| 171 |
|
|---|
| 172 | av_format_ctx->oformat->video_codec = CODEC_ID_NONE;
|
|---|
| 173 | av_format_ctx->video_codec_id = CODEC_ID_NONE;
|
|---|
| 174 | av_format_ctx->oformat->audio_codec = CODEC_ID_NONE;
|
|---|
| 175 | av_format_ctx->audio_codec_id = CODEC_ID_NONE;
|
|---|
| 176 |
|
|---|
| 177 | res = avio_open(&av_format_ctx->pb, "output.avi", AVIO_FLAG_WRITE);
|
|---|
| 178 | assert(res >= 0);
|
|---|
| 179 |
|
|---|
| 180 | video_codec = avcodec_find_encoder(CODEC_ID_H263);
|
|---|
| 181 | assert(video_codec != NULL);
|
|---|
| 182 |
|
|---|
| 183 | av_format_ctx->oformat->video_codec = CODEC_ID_H263;
|
|---|
| 184 | av_format_ctx->video_codec_id = CODEC_ID_H263;
|
|---|
| 185 |
|
|---|
| 186 | av_stream = avformat_new_stream(av_format_ctx, video_codec);
|
|---|
| 187 | assert(av_stream != NULL);
|
|---|
| 188 | av_stream->id = 0;
|
|---|
| 189 |
|
|---|
| 190 | av_stream->r_frame_rate = av_d2q(1000.0 / pts_increment, USHRT_MAX);
|
|---|
| 191 | av_stream->time_base.num = av_stream->r_frame_rate.den;
|
|---|
| 192 | av_stream->time_base.den = av_stream->r_frame_rate.num;
|
|---|
| 193 |
|
|---|
| 194 | av_video_codec_ctx = av_stream->codec;
|
|---|
| 195 |
|
|---|
| 196 | av_video_codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
|
|---|
| 197 | av_video_codec_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
|
|---|
| 198 | av_video_codec_ctx->time_base.num = 1;
|
|---|
| 199 | av_video_codec_ctx->time_base.den = 1000;
|
|---|
| 200 | av_video_codec_ctx->width = width;
|
|---|
| 201 | av_video_codec_ctx->height = height;
|
|---|
| 202 | av_video_codec_ctx->pix_fmt = PIX_FMT_YUV420P;
|
|---|
| 203 |
|
|---|
| 204 | res = avformat_write_header(av_format_ctx, NULL);
|
|---|
| 205 | assert(res >= 0);
|
|---|
| 206 |
|
|---|
| 207 | return av_format_ctx;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | void destroy_format_context(AVFormatContext* p)
|
|---|
| 211 | {
|
|---|
| 212 | unsigned int i = 0;
|
|---|
| 213 | if (p)
|
|---|
| 214 | {
|
|---|
| 215 | av_write_trailer(p);
|
|---|
| 216 | if (p->pb)
|
|---|
| 217 | {
|
|---|
| 218 | avio_sync(p->pb);
|
|---|
| 219 | avio_close(p->pb);
|
|---|
| 220 | p->pb = NULL;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | for (i = 0; i < p->nb_streams; ++i)
|
|---|
| 224 | {
|
|---|
| 225 | if (p->streams[i]->codec->internal) /* this field is only set when the codec is opened */
|
|---|
| 226 | avcodec_close(p->streams[i]->codec);
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | if (p->iformat)
|
|---|
| 230 | {
|
|---|
| 231 | avformat_close_input(&p);
|
|---|
| 232 | }
|
|---|
| 233 | else
|
|---|
| 234 | {
|
|---|
| 235 | if (p->oformat && (p->oformat->flags & AVFMT_NOFILE) == 0 && (p->oformat->flags & AVFMT_FLAG_CUSTOM_IO) == 0 && p->pb)
|
|---|
| 236 | {
|
|---|
| 237 | avio_close(p->pb);
|
|---|
| 238 | p->pb = NULL;
|
|---|
| 239 | }
|
|---|
| 240 | avformat_free_context(p);
|
|---|
| 241 | }
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | void write_video(AVFormatContext* av_format_ctx, uint8_t* data, unsigned int size, uint64_t pts, uint64_t dts, unsigned int keyframe)
|
|---|
| 246 | {
|
|---|
| 247 | int res = 0;
|
|---|
| 248 |
|
|---|
| 249 | /*
|
|---|
| 250 | * Note: Reconstructing AVPacket is essential because in real life the encoded data is not passed directly to libavformat/libavcodec.
|
|---|
| 251 | * It may be sent over network, for example.
|
|---|
| 252 | */
|
|---|
| 253 | AVPacket packet;
|
|---|
| 254 |
|
|---|
| 255 | printf("Encoded video, pts: %u, dts: %u, size: %u\n", (unsigned int)pts, (unsigned int)dts, size);
|
|---|
| 256 |
|
|---|
| 257 | init_packet(&packet);
|
|---|
| 258 | packet.stream_index = 0;
|
|---|
| 259 | packet.data = data;
|
|---|
| 260 | packet.size = size;
|
|---|
| 261 | packet.pts = pts;
|
|---|
| 262 | packet.dts = dts;
|
|---|
| 263 | packet.flags = keyframe ? AV_PKT_FLAG_KEY : 0;
|
|---|
| 264 |
|
|---|
| 265 | res = av_interleaved_write_frame(av_format_ctx, &packet);
|
|---|
| 266 | assert(res == 0);
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | int main(int argc, char* argv[])
|
|---|
| 270 | {
|
|---|
| 271 | AVCodecContext* av_ctx = NULL;
|
|---|
| 272 | AVFrame in_frame;
|
|---|
| 273 | size_t raw_size = 0;
|
|---|
| 274 | AVPacket out_packet;
|
|---|
| 275 | int got_packet = 0, res = 0;
|
|---|
| 276 | AVFormatContext* av_format_ctx = NULL;
|
|---|
| 277 | unsigned int pts = 0;
|
|---|
| 278 | int request_mb_info = 1;
|
|---|
| 279 | uint8_t* out_buffer = NULL;
|
|---|
| 280 |
|
|---|
| 281 | if (argc >= 2 && strcmp(argv[1], "--without_side_data") == 0)
|
|---|
| 282 | request_mb_info = 0;
|
|---|
| 283 |
|
|---|
| 284 | av_register_all();
|
|---|
| 285 |
|
|---|
| 286 | av_ctx = create_encoder_context(request_mb_info);
|
|---|
| 287 |
|
|---|
| 288 | init_frame(&in_frame, &raw_size);
|
|---|
| 289 | init_packet(&out_packet);
|
|---|
| 290 |
|
|---|
| 291 | out_buffer = av_malloc(raw_size * 4u);
|
|---|
| 292 | assert(out_buffer != NULL);
|
|---|
| 293 |
|
|---|
| 294 | av_format_ctx = create_format_context();
|
|---|
| 295 |
|
|---|
| 296 | for (pts = 0; pts < 5000; pts += pts_increment)
|
|---|
| 297 | {
|
|---|
| 298 | in_frame.pts = pts;
|
|---|
| 299 | in_frame.pict_type = (enum AVPictureType)0; /* AV_PICTURE_TYPE_NONE */
|
|---|
| 300 | generate_frame(&in_frame, raw_size, pts);
|
|---|
| 301 |
|
|---|
| 302 | out_packet.data = out_buffer;
|
|---|
| 303 | out_packet.size = raw_size * 4u;
|
|---|
| 304 |
|
|---|
| 305 | got_packet = 0;
|
|---|
| 306 | res = avcodec_encode_video2(av_ctx, &out_packet, &in_frame, &got_packet);
|
|---|
| 307 | assert(res == 0);
|
|---|
| 308 |
|
|---|
| 309 | if (!got_packet)
|
|---|
| 310 | continue;
|
|---|
| 311 |
|
|---|
| 312 | /* This assert fails with ffmpeg and not with libav. av_packet_get_side_data doesn't find the merged side data.
|
|---|
| 313 | if (request_mb_info)
|
|---|
| 314 | assert(av_packet_get_side_data(&out_packet, AV_PKT_DATA_H263_MB_INFO, NULL));
|
|---|
| 315 | */
|
|---|
| 316 | /*
|
|---|
| 317 | * Note: since we provided our own buffer in AVPacket to avcodec_encode_video2, out_packet.data must be equal to out_buffer here.
|
|---|
| 318 | * This is according to the avcodec_encode_video2 documentation. In reality it is not, av_packet_merge_side_data() allocates a new buffer and
|
|---|
| 319 | * writes the whole frame and side data to the new buffer. The old buffer gets released and its memory only contain the encoded frame + junk at the end.
|
|---|
| 320 | *
|
|---|
| 321 | * This works as intended with libav, which uses the buffer supplied in AVPacket.
|
|---|
| 322 | */
|
|---|
| 323 | write_video(av_format_ctx, out_buffer, out_packet.size, out_packet.pts, out_packet.dts, (out_packet.flags & AV_PKT_FLAG_KEY) != 0);
|
|---|
| 324 |
|
|---|
| 325 | out_packet.data = NULL;
|
|---|
| 326 | out_packet.size = 0;
|
|---|
| 327 | av_free_packet(&out_packet);
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | destroy_format_context(av_format_ctx);
|
|---|
| 331 | destroy_encoder_context(av_ctx);
|
|---|
| 332 |
|
|---|
| 333 | return 0;
|
|---|
| 334 | }
|
|---|