options: move -name and -title to option struct
[mplayer/greg.git] / libmpcodecs / vd_theora.c
blob5ad3b356911068674edd60df458a1ec9ffe860cd
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <assert.h>
24 #include "config.h"
25 #include "mp_msg.h"
27 #include "vd_internal.h"
29 #include "ffmpeg_files/intreadwrite.h"
31 static const vd_info_t info = {
32 "Theora/VP3",
33 "theora",
34 "David Kuehling",
35 "www.theora.org",
36 "Theora project's VP3 codec"
39 LIBVD_EXTERN(theora)
41 #include <theora/theora.h>
43 #define THEORA_NUM_HEADER_PACKETS 3
45 typedef struct theora_struct_st {
46 theora_state st;
47 theora_comment cc;
48 theora_info inf;
49 } theora_struct_t;
51 /** Convert Theora pixelformat to the corresponding IMGFMT_ */
52 static uint32_t theora_pixelformat2imgfmt(theora_pixelformat fmt){
53 switch(fmt) {
54 case OC_PF_420: return IMGFMT_YV12;
55 case OC_PF_422: return IMGFMT_422P;
56 case OC_PF_444: return IMGFMT_444P;
58 return 0;
61 // to set/get/query special features/parameters
62 static int control(sh_video_t *sh,int cmd,void* arg,...){
63 theora_struct_t *context = sh->context;
64 switch(cmd) {
65 case VDCTRL_QUERY_FORMAT:
66 if (*(int*)arg == theora_pixelformat2imgfmt(context->inf.pixelformat))
67 return CONTROL_TRUE;
68 return CONTROL_FALSE;
71 return CONTROL_UNKNOWN;
75 * init driver
77 static int init(sh_video_t *sh){
78 theora_struct_t *context = NULL;
79 uint8_t *extradata = (uint8_t *)(sh->bih + 1);
80 int extradata_size = sh->bih->biSize - sizeof(*sh->bih);
81 int errorCode = 0;
82 ogg_packet op;
83 int i;
85 context = calloc (sizeof (theora_struct_t), 1);
86 sh->context = context;
87 if (!context)
88 goto err_out;
90 theora_info_init(&context->inf);
91 theora_comment_init(&context->cc);
93 /* Read all header packets, pass them to theora_decode_header. */
94 for (i = 0; i < THEORA_NUM_HEADER_PACKETS; i++)
96 if (extradata_size > 2) {
97 op.bytes = AV_RB16(extradata);
98 op.packet = extradata + 2;
99 op.b_o_s = 1;
100 if (extradata_size < op.bytes + 2) {
101 mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Theora header too small\n");
102 goto err_out;
104 extradata += op.bytes + 2;
105 extradata_size -= op.bytes + 2;
106 } else {
107 op.bytes = ds_get_packet (sh->ds, &op.packet);
108 op.b_o_s = 1;
111 if ( (errorCode = theora_decode_header (&context->inf, &context->cc, &op)) )
113 mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Broken Theora header; errorCode=%i!\n", errorCode);
114 goto err_out;
118 /* now init codec */
119 errorCode = theora_decode_init (&context->st, &context->inf);
120 if (errorCode)
122 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Theora decode init failed: %i \n", errorCode);
123 goto err_out;
126 if(sh->aspect==0.0 && context->inf.aspect_denominator!=0)
128 sh->aspect = ((double)context->inf.aspect_numerator * context->inf.width)/
129 ((double)context->inf.aspect_denominator * context->inf.height);
132 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: Theora video init ok!\n");
133 mp_msg(MSGT_DECVIDEO,MSGL_INFO,"Frame: %dx%d, Picture %dx%d, Offset [%d,%d]\n", context->inf.width, context->inf.height, context->inf.frame_width, context->inf.frame_height, context->inf.offset_x, context->inf.offset_y);
135 return mpcodecs_config_vo (sh,context->inf.width,context->inf.height,theora_pixelformat2imgfmt(context->inf.pixelformat));
137 err_out:
138 free(context);
139 sh->context = NULL;
140 return 0;
144 * uninit driver
146 static void uninit(sh_video_t *sh)
148 theora_struct_t *context = sh->context;
150 if (context)
152 theora_info_clear(&context->inf);
153 theora_comment_clear(&context->cc);
154 theora_clear (&context->st);
155 free (context);
160 * decode frame
162 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags)
164 theora_struct_t *context = sh->context;
165 int errorCode = 0;
166 ogg_packet op;
167 yuv_buffer yuv;
168 mp_image_t* mpi;
170 // no delayed frames
171 if (!data || !len)
172 return NULL;
174 memset (&op, 0, sizeof (op));
175 op.bytes = len;
176 op.packet = data;
177 op.granulepos = -1;
179 errorCode = theora_decode_packetin (&context->st, &op);
180 if (errorCode)
182 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Theora decode packetin failed: %i \n",
183 errorCode);
184 return NULL;
187 errorCode = theora_decode_YUVout (&context->st, &yuv);
188 if (errorCode)
190 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Theora decode YUVout failed: %i \n",
191 errorCode);
192 return NULL;
195 mpi = mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0, yuv.y_width, yuv.y_height);
196 if(!mpi) return NULL;
198 mpi->planes[0]=yuv.y;
199 mpi->stride[0]=yuv.y_stride;
200 mpi->planes[1]=yuv.u;
201 mpi->stride[1]=yuv.uv_stride;
202 mpi->planes[2]=yuv.v;
203 mpi->stride[2]=yuv.uv_stride;
205 return mpi;