Index: pjproject/trunk/pjmedia/src/pjmedia-videodev/avi_dev.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia-videodev/avi_dev.c	(revision 4034)
+++ pjproject/trunk/pjmedia/src/pjmedia-videodev/avi_dev.c	(revision 4047)
@@ -47,4 +47,7 @@
     pjmedia_port                *vid;
     avi_dev_strm		*strm;
+    pjmedia_vid_codec           *codec;
+    pj_uint8_t                  *enc_buf;
+    pj_size_t                    enc_buf_size;
 };
 
@@ -70,7 +73,4 @@
     pjmedia_vid_dev_cb		     vid_cb;	    /**< Stream callback.   */
     void			    *user_data;	    /**< Application data.  */
-    pjmedia_vid_codec               *codec;
-    pj_uint8_t                      *enc_buf;
-    pj_size_t                        enc_buf_size;
 };
 
@@ -273,4 +273,9 @@
     }
 
+    if (adi->codec) {
+        pjmedia_vid_codec_close(adi->codec);
+        adi->codec = NULL;
+    }
+
     if (adi->pool)
 	pj_pool_release(adi->pool);
@@ -361,4 +366,6 @@
     unsigned local_idx;
     struct avi_dev_info *adi = NULL;
+    pjmedia_format avi_fmt;
+    const pjmedia_video_format_info *vfi;
     pj_status_t status;
 
@@ -410,4 +417,54 @@
 		adi->fpath.ptr));
 	goto on_error;
+    }
+
+    pjmedia_format_copy(&avi_fmt, &adi->vid->info.fmt);
+    vfi = pjmedia_get_video_format_info(NULL, avi_fmt.id);
+    /* Check whether the frame is encoded. */
+    if (!vfi || vfi->bpp == 0) {
+        /* Yes, prepare codec */
+        const pjmedia_vid_codec_info *codec_info;
+        pjmedia_vid_codec_param codec_param;
+	pjmedia_video_apply_fmt_param vafp;
+
+        /* Lookup codec */
+        status = pjmedia_vid_codec_mgr_get_codec_info2(NULL,
+                                                       avi_fmt.id,
+                                                       &codec_info);
+        if (status != PJ_SUCCESS || !codec_info)
+            goto on_error;
+
+        status = pjmedia_vid_codec_mgr_get_default_param(NULL, codec_info,
+                                                         &codec_param);
+        if (status != PJ_SUCCESS)
+            goto on_error;
+
+        /* Open codec */
+        status = pjmedia_vid_codec_mgr_alloc_codec(NULL, codec_info,
+                                                   &adi->codec);
+        if (status != PJ_SUCCESS)
+            goto on_error;
+
+        status = pjmedia_vid_codec_init(adi->codec, adi->pool);
+        if (status != PJ_SUCCESS)
+            goto on_error;
+
+        codec_param.dir = PJMEDIA_DIR_DECODING;
+        codec_param.packing = PJMEDIA_VID_PACKING_WHOLE;
+        status = pjmedia_vid_codec_open(adi->codec, &codec_param);
+        if (status != PJ_SUCCESS)
+            goto on_error;
+
+	/* Allocate buffer */
+        avi_fmt.id = codec_info->dec_fmt_id[0];
+        vfi = pjmedia_get_video_format_info(NULL, avi_fmt.id);
+	pj_bzero(&vafp, sizeof(vafp));
+	vafp.size = avi_fmt.det.vid.size;
+	status = vfi->apply_fmt(vfi, &vafp);
+	if (status != PJ_SUCCESS)
+	    goto on_error;
+
+	adi->enc_buf = pj_pool_alloc(adi->pool, vafp.framebytes);
+	adi->enc_buf_size = vafp.framebytes;
     }
 
@@ -437,5 +494,5 @@
     adi->info.caps = PJMEDIA_VID_DEV_CAP_FORMAT;
     adi->info.fmt_cnt = 1;
-    pjmedia_format_copy(&adi->info.fmt[0], &adi->vid->info.fmt);
+    pjmedia_format_copy(&adi->info.fmt[0], &avi_fmt);
 
     /* Set out vars */
@@ -449,4 +506,8 @@
 
 on_error:
+    if (adi->codec) {
+        pjmedia_vid_codec_close(adi->codec);
+        adi->codec = NULL;
+    }
     if (adi->pool) {
 	pj_pool_release(adi->pool);
@@ -470,7 +531,4 @@
     struct avi_dev_info *adi;
     struct avi_dev_strm *strm;
-    pjmedia_format avi_fmt;
-    const pjmedia_video_format_info *vfi;
-    pj_status_t status = PJ_SUCCESS;
 
     PJ_ASSERT_RETURN(f && param && p_vid_strm, PJ_EINVAL);
@@ -498,54 +556,5 @@
     strm->adi = adi;
 
-    pjmedia_format_copy(&avi_fmt, &adi->vid->info.fmt);
-    vfi = pjmedia_get_video_format_info(NULL, avi_fmt.id);
-    /* Check whether the frame is encoded. */
-    if (!vfi || vfi->bpp == 0) {
-        /* Yes, prepare codec */
-        const pjmedia_vid_codec_info *codec_info;
-        pjmedia_vid_codec_param codec_param;
-	pjmedia_video_apply_fmt_param vafp;
-
-        /* Lookup codec */
-        status = pjmedia_vid_codec_mgr_get_codec_info2(NULL,
-                                                       avi_fmt.id,
-                                                       &codec_info);
-        if (status != PJ_SUCCESS || !codec_info)
-            goto on_error;
-
-        status = pjmedia_vid_codec_mgr_get_default_param(NULL, codec_info,
-                                                         &codec_param);
-        if (status != PJ_SUCCESS)
-            goto on_error;
-
-        /* Open codec */
-        status = pjmedia_vid_codec_mgr_alloc_codec(NULL, codec_info,
-                                                   &strm->codec);
-        if (status != PJ_SUCCESS)
-            goto on_error;
-
-        status = pjmedia_vid_codec_init(strm->codec, strm->pool);
-        if (status != PJ_SUCCESS)
-            goto on_error;
-
-        codec_param.dir = PJMEDIA_DIR_DECODING;
-        codec_param.packing = PJMEDIA_VID_PACKING_WHOLE;
-        status = pjmedia_vid_codec_open(strm->codec, &codec_param);
-        if (status != PJ_SUCCESS)
-            goto on_error;
-
-	/* Allocate buffer */
-        avi_fmt.id = codec_info->dec_fmt_id[0];
-        vfi = pjmedia_get_video_format_info(NULL, avi_fmt.id);
-	pj_bzero(&vafp, sizeof(vafp));
-	vafp.size = avi_fmt.det.vid.size;
-	status = vfi->apply_fmt(vfi, &vafp);
-	if (status != PJ_SUCCESS)
-	    goto on_error;
-
-	strm->enc_buf = pj_pool_alloc(strm->pool, vafp.framebytes);
-	strm->enc_buf_size = vafp.framebytes;
-    }
-    pjmedia_format_copy(&param->fmt, &avi_fmt);
+    pjmedia_format_copy(&param->fmt, &adi->info.fmt[0]);
 
     /* Done */
@@ -555,8 +564,4 @@
 
     return PJ_SUCCESS;
-
-on_error:
-    avi_dev_strm_destroy(&strm->base);
-    return status;
 }
 
@@ -612,15 +617,15 @@
     struct avi_dev_strm *stream = (struct avi_dev_strm*)strm;
     
-    if (stream->codec) {
+    if (stream->adi->codec) {
         pjmedia_frame enc_frame;
         pj_status_t status;
 
-        enc_frame.buf = stream->enc_buf;
-        enc_frame.size = stream->enc_buf_size;
+        enc_frame.buf = stream->adi->enc_buf;
+        enc_frame.size = stream->adi->enc_buf_size;
         status = pjmedia_port_get_frame(stream->adi->vid, &enc_frame);
         if (status != PJ_SUCCESS)
             return status;
 
-        return pjmedia_vid_codec_decode(stream->codec, 1, &enc_frame,
+        return pjmedia_vid_codec_decode(stream->adi->codec, 1, &enc_frame,
                                         frame->size, frame);
     } else {
@@ -663,9 +668,4 @@
     avi_dev_strm_stop(strm);
 
-    if (stream->codec) {
-        pjmedia_vid_codec_close(stream->codec);
-        stream->codec = NULL;
-    }
-
     stream->adi->strm = NULL;
     stream->adi = NULL;
