summaryrefslogtreecommitdiffstats
path: root/libmpcodecs/vf_fame.c
blob: c62bef21b2c955f919c5de5f9d1f7b3762847460 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "../config.h"
#include "../mp_msg.h"

//#ifdef USE_LIBFAME

// 100=best >=80 very good >=50 fast
#define QUALITY 90

#include "../libvo/img_format.h"
#include "../mp_image.h"
#include "vf.h"

//#include "../libvo/fastmemcpy.h"
#include "../libfame/fame.h"

struct vf_priv_s {
    unsigned char* outbuf;
    int outbuf_size;
    fame_parameters_t params;
    fame_context_t *ctx;
    vo_mpegpes_t pes;
};

//===========================================================================//

static int config(struct vf_instance_s* vf,
        int width, int height, int d_width, int d_height,
	unsigned int flags, unsigned int outfmt){
    if(vf_next_query_format(vf,IMGFMT_MPEGPES)<=0) return 0;

    vf->priv->params.width=width;
    vf->priv->params.height=height;

    vf->priv->outbuf_size=10000+width*height;  // must be enough!
    if(vf->priv->outbuf) free(vf->priv->outbuf);
    vf->priv->outbuf = malloc(vf->priv->outbuf_size);

    fame_init(vf->priv->ctx,&vf->priv->params,vf->priv->outbuf,vf->priv->outbuf_size);

    return vf_next_config(vf,width,height,d_width,d_height,flags,IMGFMT_MPEGPES);
}

static void put_image(struct vf_instance_s* vf, mp_image_t *mpi){
    fame_yuv_t yuv;
    mp_image_t *dmpi;
    int out_size;

    yuv.w=mpi->width;
    yuv.h=mpi->height;
    yuv.y=mpi->planes[0];
    yuv.u=mpi->planes[1];
    yuv.v=mpi->planes[2];

    out_size = fame_encode_frame(vf->priv->ctx, &yuv, NULL);
    
    if(out_size<=0) return;

    dmpi=vf_get_image(vf->next,IMGFMT_MPEGPES,
	MP_IMGTYPE_EXPORT, 0,
	mpi->w, mpi->h);
    
    vf->priv->pes.data=vf->priv->outbuf;
    vf->priv->pes.size=out_size;
    vf->priv->pes.id=0x1E0;
    vf->priv->pes.timestamp=-1; // dunno
    
    dmpi->planes[0]=&vf->priv->pes;
    
    vf_next_put_image(vf,dmpi);
}

//===========================================================================//

static int query_format(struct vf_instance_s* vf, unsigned int fmt){
    switch(fmt){
    case IMGFMT_YV12:
    case IMGFMT_I420:
    case IMGFMT_IYUV:
	return (vf_next_query_format(vf,fmt) & (~VFCAP_CSP_SUPPORTED_BY_HW));
    }
    return 0;
}

static int open(vf_instance_t *vf, char* args){
    vf->config=config;
    vf->put_image=put_image;
    vf->query_format=query_format;
    vf->priv=malloc(sizeof(struct vf_priv_s));
    memset(vf->priv,0,sizeof(struct vf_priv_s));

    vf->priv->ctx=fame_open();
    if(!vf->priv->ctx){
	printf("FATAL: cannot open libFAME!\n");
	return 0;
    }

    // TODO: parse args ->
    vf->priv->params.coding="I";
    vf->priv->params.quality=QUALITY;
    vf->priv->params.bitrate=0;
    vf->priv->params.slices_per_frame=1;
    vf->priv->params.frames_per_sequence=25; //0xffffffff;
    vf->priv->params.frame_rate_num=25;
    vf->priv->params.frame_rate_den=1;
    vf->priv->params.shape_quality=100;
    vf->priv->params.search_range=8; // for "IPPP" only
    vf->priv->params.verbose=0;
    vf->priv->params.profile=NULL; // TODO

    return 1;
}

vf_info_t vf_info_fame = {
    "realtime mpeg1 encoding with libFAME",
    "fame",
    "A'rpi",
    "",
    open
};

//===========================================================================//
//#endif