Exemple #1
0
void encode_loop(const char *filename, long long int frames, unsigned int delay,
                 int framerate)
{
    FILE *f;

    /* abort if file already exists */
    if ((f = fopen(filename, "r")) != NULL) {
        fclose(f);
        fprintf(stderr, "error: file '%s' already exists\n", filename);
        exit(1);
    }

    AVCodec *codec;
    AVCodecContext *c= NULL;
    int ret, got_output;
    unsigned int i = 0;
    AVFrame *frame;
    AVPacket pkt;
    uint8_t endcode[] = { 0, 0, 1, 0xb7 };

    /* find the mpeg1 video encoder */
    codec = avcodec_find_encoder(AV_CODEC_ID_H264);
    if (!codec) {
        fprintf(stderr, "Codec not found\n");
        exit(1);
    }
    c = avcodec_alloc_context3(codec);
    if (!c) {
        fprintf(stderr, "Could not allocate video codec context\n");
        exit(1);
    }
    /* put sample parameters */
  //c->bit_rate = 400000;
    /* resolution must be a multiple of two */
    c->width  = get_frame_width();
    c->height = get_frame_height();
    /* frames per second */
    c->time_base = (AVRational){1,framerate};
    c->gop_size = 10; /* emit one intra frame every ten frames */
    c->max_b_frames = 1;
    c->pix_fmt = AV_PIX_FMT_YUV420P;

    av_opt_set(c->priv_data, "preset", "slow", 0);
    av_opt_set_int(c, "crf", 20, AV_OPT_SEARCH_CHILDREN);

    /* open codec */
    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        exit(1);
    }

    /* open file */
    f = fopen(filename, "wb");
    if (!f) {
        fprintf(stderr, "Could not open %s\n", filename);
        exit(1);
    }

    /* allocate video frame */
    frame = av_frame_alloc();
    if (!frame) {
        fprintf(stderr, "Could not allocate video frame\n");
        exit(1);
    }
    frame->format = c->pix_fmt;
    frame->width  = c->width;
    frame->height = c->height;
    /* the image can be allocated by any means and av_image_alloc() is
     * just the most convenient way if av_malloc() is to be used */
    ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height,
                         c->pix_fmt, 32);
    if (ret < 0) {
        fprintf(stderr, "Could not allocate raw picture buffer\n");
        exit(1);
    }

    /* will we run forever? */
    int inf = !frames;

    while (1) {
        if (CAUGHT_SIGINT)
            break;

        if (!inf && --frames < 0)
            break;

        usleep(delay);

        av_init_packet(&pkt);
        pkt.data = NULL;    // packet data will be allocated by the encoder
        pkt.size = 0;
        fflush(stdout);

        struct SwsContext *ctx =
            sws_getContext(c->width, c->height, AV_PIX_FMT_RGB24,
                           c->width, c->height, AV_PIX_FMT_YUV420P,
                           0, 0, 0, 0);

        unsigned char *rgb_buf = grab_frame();
        const uint8_t *data_in[1] = { rgb_buf };
        int inline_size[1]  = { 3*c->width };
        sws_scale(ctx, data_in, inline_size, 0, c->height,
                  frame->data, frame->linesize);

        frame->pts = i;
        /* encode the image */
        ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
        if (ret < 0) {
            fprintf(stderr, "Error encoding frame\n");
            exit(1);
        }
        if (got_output) {
            printf("Wrote frame %d (size=%d)\n", i++, pkt.size);
            fwrite(pkt.data, 1, pkt.size, f);
            av_free_packet(&pkt);
        }

        free(rgb_buf);
        sws_freeContext(ctx);
    }
    /* get the delayed frames */
    for (got_output = 1; got_output; i++) {
        fflush(stdout);
        ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
        if (ret < 0) {
            fprintf(stderr, "Error encoding frame\n");
            exit(1);
        }
        if (got_output) {
            printf("Wrote frame %3d (size=%5d)\n", i, pkt.size);
            fwrite(pkt.data, 1, pkt.size, f);
            av_free_packet(&pkt);
        }
    }
    /* add sequence end code to have a real mpeg file */
    fwrite(endcode, 1, sizeof(endcode), f);
    fclose(f);
    avcodec_close(c);
    av_free(c);
    av_freep(&frame->data[0]);
    av_frame_free(&frame);
    printf("\n");
}
Exemple #2
0
int rs_get_detached_frame_width(const rs_frame_ref * frame_ref, rs_error ** error) try
{
    VALIDATE_NOT_NULL(frame_ref);
    return frame_ref->get_frame_width();
}