| 1 |
|
|---|
| 2 | #include <stdio.h>
|
|---|
| 3 | #include <stdlib.h>
|
|---|
| 4 | #include <pthread.h>
|
|---|
| 5 |
|
|---|
| 6 | #include "libavcodec/avcodec.h"
|
|---|
| 7 | #include "libavformat/avformat.h"
|
|---|
| 8 |
|
|---|
| 9 | int try_encode() {
|
|---|
| 10 | AVCodec* codec = avcodec_find_encoder_by_name("libx264");
|
|---|
| 11 | if (codec == NULL) {
|
|---|
| 12 | fprintf(stderr, "unable to find encoder\n");
|
|---|
| 13 | return 0;
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | AVCodecContext* codecContext = avcodec_alloc_context3(codec);
|
|---|
| 17 | if (codecContext == NULL) {
|
|---|
| 18 | fprintf(stderr, "failed to allocate codec context\n");
|
|---|
| 19 | return 0;
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | codecContext->width = 960;
|
|---|
| 23 | codecContext->height = 604;
|
|---|
| 24 | codecContext->pix_fmt = AV_PIX_FMT_YUV420P;
|
|---|
| 25 | codecContext->time_base = (AVRational){ 1, 1 };
|
|---|
| 26 | codecContext->gop_size = 1;
|
|---|
| 27 | codecContext->ticks_per_frame = 1;
|
|---|
| 28 | codecContext->thread_count = 1;
|
|---|
| 29 |
|
|---|
| 30 | AVDictionary* opts = NULL;
|
|---|
| 31 | av_dict_set(&opts, "preset", "slow", 0);
|
|---|
| 32 | av_dict_set(&opts, "tune", "psnr", 0);
|
|---|
| 33 |
|
|---|
| 34 | int openResult = avcodec_open2(codecContext, codec, &opts);
|
|---|
| 35 | if (openResult < 0) {
|
|---|
| 36 | fprintf(stderr, "failed opening video codec: %s\n", av_err2str(openResult));
|
|---|
| 37 | return 0;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | avcodec_free_context(&codecContext);
|
|---|
| 41 |
|
|---|
| 42 | return 1;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | void* thread_main(void* p) {
|
|---|
| 46 | for (int i = 0; i < 100000; i++) {
|
|---|
| 47 | try_encode();
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | int main(int argc, const char** argv) {
|
|---|
| 52 | if (argc < 2) {
|
|---|
| 53 | printf("usage: enc_test [threads]\n");
|
|---|
| 54 | return -1;
|
|---|
| 55 | }
|
|---|
| 56 | int numThreads = atoi(argv[1]);
|
|---|
| 57 | av_register_all();
|
|---|
| 58 | av_log_set_level(AV_LOG_QUIET);
|
|---|
| 59 | pthread_t* threads = malloc(sizeof(pthread_t) * numThreads);
|
|---|
| 60 | for (int i = 0; i < numThreads; i++) {
|
|---|
| 61 | pthread_create(&threads[i], NULL, &thread_main, NULL);
|
|---|
| 62 | }
|
|---|
| 63 | for (int i = 0; i < numThreads; i++) {
|
|---|
| 64 | pthread_join(threads[i], NULL);
|
|---|
| 65 | }
|
|---|
| 66 | free(threads);
|
|---|
| 67 | }
|
|---|