sync with ffmpeg
[mplayer/glamo.git] / libmpcodecs / vd_theora.c
blobeb37b97cf849886701bc8008151447f442c5fecd
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <assert.h>
6 #include "config.h"
7 #include "mp_msg.h"
8 #include "help_mp.h"
10 #include "vd_internal.h"
12 static vd_info_t info = {
13 "Theora/VP3",
14 "theora",
15 "David Kuehling",
16 "www.theora.org",
17 "Theora project's VP3 codec"
20 LIBVD_EXTERN(theora)
22 #include <theora/theora.h>
24 #define THEORA_NUM_HEADER_PACKETS 3
26 // to set/get/query special features/parameters
27 static int control(sh_video_t *sh,int cmd,void* arg,...){
28 switch(cmd) {
29 case VDCTRL_QUERY_FORMAT:
30 if ((*((int*)arg)) == IMGFMT_YV12)
31 return CONTROL_TRUE;
32 return CONTROL_FALSE;
35 return CONTROL_UNKNOWN;
38 typedef struct theora_struct_st {
39 theora_state st;
40 theora_comment cc;
41 theora_info inf;
42 } theora_struct_t;
45 * init driver
47 static int init(sh_video_t *sh){
48 theora_struct_t *context = NULL;
49 int failed = 1;
50 int errorCode = 0;
51 ogg_packet op;
52 int i;
54 /* check whether video output format is supported */
55 switch(sh->codec->outfmt[sh->outfmtidx])
57 case IMGFMT_YV12: /* well, this should work... */ break;
58 default:
59 mp_msg (MSGT_DECVIDEO,MSGL_ERR,"Unsupported out_fmt: 0x%X\n",
60 sh->codec->outfmt[sh->outfmtidx]);
61 return 0;
64 /* this is not a loop, just a context, from which we can break on error */
67 context = calloc (sizeof (theora_struct_t), 1);
68 sh->context = context;
69 if (!context)
70 break;
72 theora_info_init(&context->inf);
73 theora_comment_init(&context->cc);
75 /* Read all header packets, pass them to theora_decode_header. */
76 for (i = 0; i < THEORA_NUM_HEADER_PACKETS; i++)
78 op.bytes = ds_get_packet (sh->ds, &op.packet);
79 op.b_o_s = 1;
80 if ( (errorCode = theora_decode_header (&context->inf, &context->cc, &op)) )
82 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Broken Theora header; errorCode=%i!\n", errorCode);
83 break;
86 if (errorCode)
87 break;
89 /* now init codec */
90 errorCode = theora_decode_init (&context->st, &context->inf);
91 if (errorCode)
93 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Theora decode init failed: %i \n",
94 errorCode);
95 break;
97 failed = 0;
98 } while (0);
100 if (failed)
102 if (context)
104 free (context);
105 sh->context = NULL;
107 return 0;
110 if(sh->aspect==0.0 && context->inf.aspect_denominator!=0)
112 sh->aspect = (float)(context->inf.aspect_numerator * context->inf.frame_width)/
113 (context->inf.aspect_denominator * context->inf.frame_height);
116 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: Theora video init ok!\n");
118 return mpcodecs_config_vo (sh,context->inf.frame_width,context->inf.frame_height,IMGFMT_YV12);
122 * uninit driver
124 static void uninit(sh_video_t *sh)
126 theora_struct_t *context = (theora_struct_t *)sh->context;
128 if (context)
130 theora_info_clear(&context->inf);
131 theora_comment_clear(&context->cc);
132 theora_clear (&context->st);
133 free (context);
138 * decode frame
140 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags)
142 theora_struct_t *context = (theora_struct_t *)sh->context;
143 int errorCode = 0;
144 ogg_packet op;
145 yuv_buffer yuv;
146 mp_image_t* mpi;
148 memset (&op, 0, sizeof (op));
149 op.bytes = len;
150 op.packet = data;
151 op.granulepos = -1;
153 errorCode = theora_decode_packetin (&context->st, &op);
154 if (errorCode)
156 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Theora decode packetin failed: %i \n",
157 errorCode);
158 return NULL;
161 errorCode = theora_decode_YUVout (&context->st, &yuv);
162 if (errorCode)
164 mp_msg(MSGT_DEMUX,MSGL_ERR,"Theora decode YUVout failed: %i \n",
165 errorCode);
166 return 0;
169 mpi = mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0, yuv.y_width, yuv.y_height);
170 if(!mpi) return NULL;
172 mpi->planes[0]=yuv.y;
173 mpi->stride[0]=yuv.y_stride;
174 mpi->planes[1]=yuv.u;
175 mpi->stride[1]=yuv.uv_stride;
176 mpi->planes[2]=yuv.v;
177 mpi->stride[2]=yuv.uv_stride;
179 return mpi;