Thu, 05 Jan 2006 05:29:51 +0000
[gaim-migrate @ 15077]
jabber.c:389: warning: ISO C90 forbids mixed declarations and code
| 12024 | 1 | /* |
| 2 | The mediastreamer library aims at providing modular media processing and I/O | |
| 3 | for linphone, but also for any telephony application. | |
| 4 | Copyright (C) 2001 Simon MORLAT simon.morlat@linphone.org | |
| 5 | ||
| 6 | This library is free software; you can redistribute it and/or | |
| 7 | modify it under the terms of the GNU Lesser General Public | |
| 8 | License as published by the Free Software Foundation; either | |
| 9 | version 2.1 of the License, or (at your option) any later version. | |
| 10 | ||
| 11 | This library is distributed in the hope that it will be useful, | |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 14 | Lesser General Public License for more details. | |
| 15 | ||
| 16 | You should have received a copy of the GNU Lesser General Public | |
| 17 | License along with this library; if not, write to the Free Software | |
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | */ | |
| 20 | ||
| 21 | #include "msavencoder.h" | |
| 22 | #include "msutils.h" | |
| 23 | ||
| 24 | extern MSCodecInfo MPEG4info; | |
| 25 | extern MSCodecInfo MPEGinfo; | |
| 26 | extern MSCodecInfo h263info; | |
| 27 | ||
| 28 | static MSAVEncoderClass *ms_avencoder_class=NULL; | |
| 29 | static void ms_AVencoder_rtp_callback (AVCodecContext *ctx,void *data, int size, int packet_number); | |
| 30 | ||
| 31 | MSFilter *ms_h263_encoder_new() | |
| 32 | { | |
| 33 | return ms_AVencoder_new_with_codec(CODEC_ID_H263,&h263info); | |
| 34 | } | |
| 35 | ||
| 36 | MSFilter *ms_mpeg_encoder_new() | |
| 37 | { | |
| 38 | return ms_AVencoder_new_with_codec(CODEC_ID_MPEG1VIDEO, &MPEGinfo); | |
| 39 | } | |
| 40 | ||
| 41 | MSFilter *ms_mpeg4_encoder_new() | |
| 42 | { | |
| 43 | return ms_AVencoder_new_with_codec(CODEC_ID_MPEG4,&MPEG4info); | |
| 44 | } | |
| 45 | ||
| 46 | MSFilter * ms_AVencoder_new_with_codec(enum CodecID codec_id, MSCodecInfo *info) | |
| 47 | { | |
| 48 | MSAVEncoder *enc; | |
| 49 | AVCodec *avc; | |
| 50 | enc=g_malloc0(sizeof(MSAVEncoder)); | |
| 51 | if (ms_avencoder_class==NULL) | |
| 52 | { | |
| 53 | ms_avencoder_class=g_malloc0(sizeof(MSAVEncoderClass)); | |
| 54 | ms_AVencoder_class_init(ms_avencoder_class); | |
| 55 | } | |
| 56 | MS_FILTER(enc)->klass=(MSFilterClass*)ms_avencoder_class; | |
| 57 | avc=avcodec_find_encoder(codec_id); | |
| 58 | if (avc==NULL) g_error("unknown av codec."); | |
| 59 | ms_AVencoder_init(enc,avc); | |
| 60 | return MS_FILTER(enc); | |
| 61 | } | |
| 62 | ||
| 63 | ||
| 64 | void ms_AVencoder_init(MSAVEncoder *enc, AVCodec *codec) | |
| 65 | { | |
| 66 | gint error; | |
| 67 | AVCodecContext *c=&enc->av_context; | |
| 68 | ||
| 69 | ms_filter_init(MS_FILTER(enc)); | |
| 70 | MS_FILTER(enc)->inqueues=enc->q_inputs; | |
| 71 | MS_FILTER(enc)->outqueues=enc->q_outputs; | |
| 72 | /* put default values */ | |
| 73 | memset(c, 0, sizeof(AVCodecContext)); | |
| 74 | avcodec_get_context_defaults(c); | |
| 75 | ||
| 76 | /* put sample parameters */ | |
| 77 | c->bit_rate = 400000; | |
| 78 | /* resolution must be a multiple of two */ | |
| 79 | c->width = VIDEO_SIZE_CIF_W; | |
| 80 | c->height = VIDEO_SIZE_CIF_H; | |
| 81 | /* frames per second */ | |
| 82 | c->frame_rate = 15; | |
| 83 | c->frame_rate_base = 1; | |
| 84 | c->gop_size = 10; /* emit one intra frame every x frames */ | |
| 85 | c->rtp_mode = 1; | |
| 86 | c->rtp_payload_size = 1488; | |
| 87 | c->opaque = (void *) enc; | |
| 88 | c->rtp_callback = ms_AVencoder_rtp_callback; | |
| 89 | c->pix_fmt=PIX_FMT_YUV420P; | |
| 90 | ||
| 91 | enc->av_opened=0; | |
| 92 | enc->av_codec=codec; | |
| 93 | enc->yuv_buf=NULL; | |
| 94 | enc->comp_buf=NULL; | |
| 95 | /*set default input format */ | |
| 96 | ms_AVencoder_set_format(enc,"RGB24"); | |
| 97 | } | |
| 98 | ||
| 99 | void ms_AVencoder_class_init(MSAVEncoderClass *klass) | |
| 100 | { | |
| 101 | ms_filter_class_init(MS_FILTER_CLASS(klass)); | |
| 102 | MS_FILTER_CLASS(klass)->info=0; | |
| 103 | MS_FILTER_CLASS(klass)->max_qinputs=MSAVENCODER_MAX_INPUTS; | |
| 104 | MS_FILTER_CLASS(klass)->max_qoutputs=MSAVENCODER_MAX_OUTPUTS; | |
| 105 | MS_FILTER_CLASS(klass)->r_maxgran=0; | |
| 106 | MS_FILTER_CLASS(klass)->w_maxgran=0; | |
| 107 | MS_FILTER_CLASS(klass)->destroy=(MSFilterDestroyFunc)ms_AVencoder_destroy; | |
| 108 | MS_FILTER_CLASS(klass)->process=(MSFilterProcessFunc)ms_AVencoder_process; | |
| 109 | ms_filter_class_set_name(MS_FILTER_CLASS(klass),"AVEncoder"); | |
| 110 | ||
| 111 | } | |
| 112 | ||
| 113 | void ms_AVencoder_uninit(MSAVEncoder *enc) | |
| 114 | { | |
| 115 | if (enc->av_opened) | |
| 116 | avcodec_close(&enc->av_context); | |
| 117 | if (enc->comp_buf!=NULL) { | |
| 118 | ms_buffer_destroy(enc->comp_buf); | |
| 119 | enc->comp_buf=NULL; | |
| 120 | } | |
| 121 | if (enc->yuv_buf!=NULL) { | |
| 122 | ms_buffer_destroy(enc->yuv_buf); | |
| 123 | enc->yuv_buf=NULL; | |
| 124 | } | |
| 125 | ||
| 126 | } | |
| 127 | void ms_AVencoder_destroy( MSAVEncoder *obj) | |
| 128 | { | |
| 129 | ms_AVencoder_uninit(obj); | |
| 130 | g_free(obj); | |
| 131 | } | |
| 132 | ||
| 133 | ||
| 134 | void ms_AVencoder_set_frame_rate(MSAVEncoder *obj, gint frame_rate, gint frame_rate_base) | |
| 135 | { | |
| 136 | obj->av_context.frame_rate = frame_rate; | |
| 137 | obj->av_context.frame_rate_base = frame_rate_base; | |
| 138 | } | |
| 139 | ||
| 140 | static void ms_AVencoder_rtp_callback (AVCodecContext *ctx, void *data, int size, int packet_number) | |
| 141 | { | |
| 142 | MSAVEncoder *r = MS_AVENCODER(ctx->opaque); | |
| 143 | MSQueue *outq = r->q_outputs[0]; | |
| 144 | MSMessage *outm; | |
| 145 | guint32 *p = (guint32 *) data; | |
| 146 | gint gob_num = (ntohl(*p) >> 10) & 0x1f; | |
| 147 | ||
| 148 | /*g_message("ms_AVencoder_rtp_callback: packet %i, size %i, GOB number %i", packet_number, size, gob_num);*/ | |
| 149 | ms_trace("ms_AVencoder_rtp_callback: received %08x %08x", ntohl(p[0]), ntohl(p[1])); | |
| 150 | /* set the H.263 Payload Header (RFC 2429) */ | |
| 151 | p[0] = ntohl( (0x04000000) | (ntohl(p[0]) & 0x0000ffff) ); /* P=1, V=0, PLEN=0 */ | |
| 152 | ms_trace("ms_AVencoder_rtp_callback: sending %08x %08x", ntohl(p[0]), ntohl(p[1])); | |
| 153 | outm = ms_message_new(size); | |
| 154 | memcpy(outm->data,data,size); | |
| 155 | ms_queue_put(outq, outm); | |
| 156 | } | |
| 157 | ||
| 158 | void ms_AVencoder_process(MSAVEncoder *r) | |
| 159 | { | |
| 160 | AVFrame orig; | |
| 161 | AVFrame pict; | |
| 162 | AVCodecContext *c=&r->av_context; | |
| 163 | MSQueue *inq,*outq; | |
| 164 | MSMessage *inm,*outm; | |
| 165 | gint error; | |
| 166 | ||
| 167 | inq=r->q_inputs[0]; | |
| 168 | outq=r->q_outputs[0]; | |
| 169 | ||
| 170 | /* get a picture from the input queue */ | |
| 171 | inm=ms_queue_get(inq); | |
| 172 | g_return_if_fail(inm!=NULL); | |
| 173 | ||
| 174 | /* allocate a new image */ | |
| 175 | if (r->yuv_buf==NULL){ | |
| 176 | gint bsize = avpicture_get_size(c->pix_fmt,c->width,c->height); | |
| 177 | r->yuv_buf=ms_buffer_new(bsize); | |
| 178 | r->yuv_buf->ref_count++; | |
| 179 | ||
| 180 | r->comp_buf=ms_buffer_new(bsize/2); | |
| 181 | r->comp_buf->ref_count++; | |
| 182 | } | |
| 183 | if (!r->av_opened || r->av_context.codec == NULL){ | |
| 184 | error=avcodec_open(c, r->av_codec); | |
| 185 | ms_trace("image format is %i.",c->pix_fmt); | |
| 186 | if (error!=0) { | |
| 187 | g_warning("avcodec_open() failed: %i",error); | |
| 188 | return; | |
| 189 | }else r->av_opened=1; | |
| 190 | } | |
| 191 | outm=ms_message_alloc(); | |
| 192 | /* convert image if necessary */ | |
| 193 | if (r->input_pix_fmt!=c->pix_fmt){ | |
| 194 | ms_trace("Changing picture format."); | |
| 195 | avpicture_fill((AVPicture*)&orig,inm->data,r->input_pix_fmt,c->width,c->height); | |
| 196 | avpicture_fill((AVPicture*)&pict,r->yuv_buf->buffer,c->pix_fmt,c->width,c->height); | |
| 197 | if (img_convert((AVPicture*)&pict,c->pix_fmt,(AVPicture*)&orig,r->input_pix_fmt,c->width,c->height) < 0) { | |
| 198 | g_warning("img_convert failed"); | |
| 199 | return; | |
| 200 | } | |
| 201 | //if (pict.data[0]==NULL) g_error("img_convert failed."); | |
| 202 | ms_message_set_buf(outm,r->yuv_buf); | |
| 203 | } | |
| 204 | else | |
| 205 | { | |
| 206 | avpicture_fill((AVPicture*)&pict,inm->data,c->pix_fmt,c->width,c->height); | |
| 207 | ms_message_set_buf(outm,inm->buffer); | |
| 208 | } | |
| 209 | /* timestamp used by ffmpeg, unset here */ | |
| 210 | pict.pts=AV_NOPTS_VALUE; | |
| 211 | error=avcodec_encode_video(c, r->comp_buf->buffer, r->comp_buf->size, &pict); | |
| 212 | if (error<=0) ms_warning("ms_AVencoder_process: error %i.",error); | |
| 213 | else ms_trace("ms_AVencoder_process: video encoding done"); | |
| 214 | if (r->q_outputs[1]!=NULL) ms_queue_put(r->q_outputs[1],outm); | |
| 215 | else ms_message_destroy(outm); | |
| 216 | ms_message_destroy(inm); | |
| 217 | } | |
| 218 | ||
| 219 | gint ms_AVencoder_set_format(MSAVEncoder *enc, gchar *fmt) | |
| 220 | { | |
| 221 | gint format; | |
| 222 | if (strcmp(fmt,"YUV420P")==0) format=PIX_FMT_YUV420P; | |
| 223 | else if (strcmp(fmt,"YUV422")==0) format=PIX_FMT_YUV422; | |
| 224 | else if (strcmp(fmt,"RGB24")==0) format=PIX_FMT_RGB24; | |
| 225 | else if (strcmp(fmt,"BGR24")==0) format=PIX_FMT_BGR24; | |
| 226 | else if (strcmp(fmt,"YUV422P")==0) format=PIX_FMT_YUV422P; | |
| 227 | else if (strcmp(fmt,"YUV444P")==0) format=PIX_FMT_YUV444P; | |
| 228 | else { | |
| 229 | g_warning("ms_AVdecoder_set_format: unsupported format %s.",fmt); | |
| 230 | return -1; | |
| 231 | } | |
| 232 | enc->input_pix_fmt=format; | |
| 233 | ||
| 234 | return 0; | |
| 235 | } |