mp_msg: print messages to stdout, statusline to stderr
[mplayer.git] / libmpcodecs / vd_theora.c
blobba3c0d5d86e2cb264b752c5b2fd2d0a18ef8042c
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 <libavutil/intreadwrite.h>
26 #include "config.h"
27 #include "mp_msg.h"
29 #include "vd_internal.h"
32 static const vd_info_t info = {
33 "Theora/VP3",
34 "theora",
35 "David Kuehling",
36 "www.theora.org",
37 "Theora project's VP3 codec"
40 LIBVD_EXTERN(theora)
42 #include <theora/theora.h>
44 #define THEORA_NUM_HEADER_PACKETS 3
46 typedef struct theora_struct_st {
47 theora_state st;
48 theora_comment cc;
49 theora_info inf;
50 } theora_struct_t;
52 /** Convert Theora pixelformat to the corresponding IMGFMT_ */
53 static uint32_t theora_pixelformat2imgfmt(theora_pixelformat fmt){
54 switch(fmt) {
55 case OC_PF_420: return IMGFMT_YV12;
56 case OC_PF_422: return IMGFMT_422P;
57 case OC_PF_444: return IMGFMT_444P;
59 return 0;
62 // to set/get/query special features/parameters
63 static int control(sh_video_t *sh,int cmd,void* arg,...){
64 theora_struct_t *context = sh->context;
65 switch(cmd) {
66 case VDCTRL_QUERY_FORMAT:
67 if (*(int*)arg == theora_pixelformat2imgfmt(context->inf.pixelformat))
68 return CONTROL_TRUE;
69 return CONTROL_FALSE;
72 return CONTROL_UNKNOWN;
76 * init driver
78 static int init(sh_video_t *sh){
79 theora_struct_t *context = NULL;
80 uint8_t *extradata = (uint8_t *)(sh->bih + 1);
81 int extradata_size = sh->bih->biSize - sizeof(*sh->bih);
82 int errorCode = 0;
83 ogg_packet op;
84 int i;
86 context = calloc (sizeof (theora_struct_t), 1);
87 sh->context = context;
88 if (!context)
89 goto err_out;
91 theora_info_init(&context->inf);
92 theora_comment_init(&context->cc);
94 /* Read all header packets, pass them to theora_decode_header. */
95 for (i = 0; i < THEORA_NUM_HEADER_PACKETS; i++)
97 if (extradata_size > 2) {
98 op.bytes = AV_RB16(extradata);
99 op.packet = extradata + 2;
100 op.b_o_s = 1;
101 if (extradata_size < op.bytes + 2) {
102 mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Theora header too small\n");
103 goto err_out;
105 extradata += op.bytes + 2;
106 extradata_size -= op.bytes + 2;
107 } else {
108 op.bytes = ds_get_packet (sh->ds, &op.packet);
109 op.b_o_s = 1;
112 if ( (errorCode = theora_decode_header (&context->inf, &context->cc, &op)) )
114 mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Broken Theora header; errorCode=%i!\n", errorCode);
115 goto err_out;
119 /* now init codec */
120 errorCode = theora_decode_init (&context->st, &context->inf);
121 if (errorCode)
123 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Theora decode init failed: %i \n", errorCode);
124 goto err_out;
127 if(sh->aspect==0.0 && context->inf.aspect_denominator!=0)
129 sh->aspect = ((double)context->inf.aspect_numerator * context->inf.width)/
130 ((double)context->inf.aspect_denominator * context->inf.height);
133 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: Theora video init ok!\n");
134 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);
136 return mpcodecs_config_vo (sh,context->inf.width,context->inf.height,theora_pixelformat2imgfmt(context->inf.pixelformat));
138 err_out:
139 free(context);
140 sh->context = NULL;
141 return 0;
145 * uninit driver
147 static void uninit(sh_video_t *sh)
149 theora_struct_t *context = sh->context;
151 if (context)
153 theora_info_clear(&context->inf);
154 theora_comment_clear(&context->cc);
155 theora_clear (&context->st);
156 free (context);
161 * decode frame
163 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags)
165 theora_struct_t *context = sh->context;
166 int errorCode = 0;
167 ogg_packet op;
168 yuv_buffer yuv;
169 mp_image_t* mpi;
171 // no delayed frames
172 if (!data || !len)
173 return NULL;
175 memset (&op, 0, sizeof (op));
176 op.bytes = len;
177 op.packet = data;
178 op.granulepos = -1;
180 errorCode = theora_decode_packetin (&context->st, &op);
181 if (errorCode)
183 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Theora decode packetin failed: %i \n",
184 errorCode);
185 return NULL;
188 errorCode = theora_decode_YUVout (&context->st, &yuv);
189 if (errorCode)
191 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"Theora decode YUVout failed: %i \n",
192 errorCode);
193 return NULL;
196 mpi = mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0, yuv.y_width, yuv.y_height);
197 if(!mpi) return NULL;
199 mpi->planes[0]=yuv.y;
200 mpi->stride[0]=yuv.y_stride;
201 mpi->planes[1]=yuv.u;
202 mpi->stride[1]=yuv.uv_stride;
203 mpi->planes[2]=yuv.v;
204 mpi->stride[2]=yuv.uv_stride;
206 return mpi;