void decode_frame(State *state, AVPacket *pkt, int *got_frame, int64_t desired_frame_number) {
	AVFrame *frame = NULL;
	
	*got_frame = 0;
	
	// Read frames and return the first one found
	while (av_read_frame(state->pFormatCtx, pkt) >= 0) {

		// Is this a packet from the video stream?
		if (pkt->stream_index == state->video_stream) {
			int codec_id = state->video_st->codec->codec_id;
			        		
			// If the image isn't already in a supported format convert it to one
			if (!is_supported_format(codec_id)) {
	            *got_frame = 0;
	            
	        	// Allocate video frame
	            frame = avcodec_alloc_frame();

	            if (!frame) {
	            	break;
	            }
	            
				// Decode video frame
				if (avcodec_decode_video2(state->video_st->codec, frame, got_frame, pkt) <= 0) {
					*got_frame = 0;
					break;
				}

				// Did we get a video frame?
				if (*got_frame) {
					if (desired_frame_number == -1 ||
							(desired_frame_number != -1 && frame->pkt_pts >= desired_frame_number)) {
						AVPacket packet;
					    av_init_packet(&packet);
        	            packet.data = NULL;
        	            packet.size = 0;
						convert_image(state->video_st->codec, frame, &packet, got_frame);
						*pkt = packet;
						break;
					}
				}
			} else {
				*got_frame = 1;
	        	break;
			}
		}
	}
	
	// Free the frame
	av_free(frame);
}
void decode_frame(State *state, AVPacket *pkt, int *got_frame, int64_t desired_frame_number, int width, int height) {
	// Allocate video frame
	AVFrame *frame = av_frame_alloc();

	*got_frame = 0;
	
	if (!frame) {
	    return;
	}
	
	// Read frames and return the first one found
	while (av_read_frame(state->pFormatCtx, pkt) >= 0) {

		// Is this a packet from the video stream?
		if (pkt->stream_index == state->video_stream) {
			int codec_id = state->video_st->codec->codec_id;
			int pix_fmt = state->video_st->codec->pix_fmt;
			        		
			// If the image isn't already in a supported format convert it to one
			if (!is_supported_format(codec_id, pix_fmt)) {
	            *got_frame = 0;
	            
				// Decode video frame
				if (avcodec_decode_video2(state->video_st->codec, frame, got_frame, pkt) <= 0) {
					*got_frame = 0;
					break;
				}

				// Did we get a video frame?
				if (*got_frame) {
					if (desired_frame_number == -1 ||
							(desired_frame_number != -1 && frame->pkt_pts >= desired_frame_number)) {
						if (pkt->data) {
							av_packet_unref(pkt);
						}
					    av_init_packet(pkt);
						convert_image(state, state->video_st->codec, frame, pkt, got_frame, width, height);
						break;
					}
				}
			} else {
				*got_frame = 1;
	        	break;
			}
		}
	}
	
	// Free the frame
	av_frame_free(&frame);
}
int get_embedded_picture(State **ps, AVPacket *pkt) {
    printf("get_embedded_picture\n");
    int i = 0;
    int got_packet = 0;
    AVFrame *frame = NULL;
    
    State *state = *ps;
    
    if (!state || !state->pFormatCtx) {
        return FAILURE;
    }
    
    // read the format headers
    if (state->pFormatCtx->iformat->read_header(state->pFormatCtx) < 0) {
        printf("Could not read the format header\n");
        return FAILURE;
    }
    
    // find the first attached picture, if available
    for (i = 0; i < state->pFormatCtx->nb_streams; i++) {
        if (state->pFormatCtx->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC) {
            printf("Found album art\n");
            *pkt = state->pFormatCtx->streams[i]->attached_pic;
            
            // Is this a packet from the video stream?
            if (pkt->stream_index == state->video_stream) {
                int codec_id = state->video_st->codec->codec_id;
                
                // If the image isn't already in a supported format convert it to one
                if (!is_supported_format(codec_id)) {
                    int got_frame = 0;
                    
                    av_init_packet(pkt);
                    
                    frame = av_frame_alloc();
                    
                    if (!frame) {
                        break;
                    }
                    
                    if (avcodec_decode_video2(state->video_st->codec, frame, &got_frame, pkt) <= 0) {
                        break;
                    }
                    
                    // Did we get a video frame?
                    if (got_frame) {
                        AVPacket packet;
                        av_init_packet(&packet);
                        packet.data = NULL;
                        packet.size = 0;
                        convert_image(state->video_st->codec, frame, &packet, &got_packet, -1, -1);
                        *pkt = packet;
                        break;
                    }
                } else {
                    av_init_packet(pkt);
                    pkt->data = state->pFormatCtx->streams[i]->attached_pic.data;
                    pkt->size = state->pFormatCtx->streams[i]->attached_pic.size;
                    
                    got_packet = 1;
                    break;
                }
            }
        }
    }
    
    av_free(frame);
    
    if (got_packet) {
        return SUCCESS;
    } else {
        return FAILURE;
    }
}
int get_embedded_picture(State **ps, AVPacket *pkt) {
	printf("get_embedded_picture\n");
	int i = 0;
	int got_packet = 0;
	AVFrame *frame = NULL;
	
	State *state = *ps;

	if (!state || !state->pFormatCtx) {
		return FAILURE;
	}

    // TODO commented out 5/31/16, do we actully need this since the context
    // has been initialized
    // read the format headers
    /*if (state->pFormatCtx->iformat->read_header(state->pFormatCtx) < 0) {
    	printf("Could not read the format header\n");
    	return FAILURE;
    }*/

    // find the first attached picture, if available
    for (i = 0; i < state->pFormatCtx->nb_streams; i++) {
        if (state->pFormatCtx->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC) {
        	printf("Found album art\n");
        	if (pkt) {
        		av_packet_unref(pkt);
        		av_init_packet(pkt);
        	}
            av_copy_packet(pkt, &state->pFormatCtx->streams[i]->attached_pic);
			// TODO is this right
			got_packet = 1;
        	
        	// Is this a packet from the video stream?
        	if (pkt->stream_index == state->video_stream) {
        		int codec_id = state->video_st->codec->codec_id;
				int pix_fmt = state->video_st->codec->pix_fmt;

        		// If the image isn't already in a supported format convert it to one
        		if (!is_supported_format(codec_id, pix_fmt)) {
        			int got_frame = 0;
        			
   			        frame = av_frame_alloc();
        			    	
   			        if (!frame) {
   			        	break;
        			}
   			        
        			if (avcodec_decode_video2(state->video_st->codec, frame, &got_frame, pkt) <= 0) {
        				break;
        			}

        			// Did we get a video frame?
        			if (got_frame) {
        				AVPacket convertedPkt;
        	            av_init_packet(&convertedPkt);
        	            convertedPkt.size = 0;
        	            convertedPkt.data = NULL;

        	            convert_image(state, state->video_st->codec, frame, &convertedPkt, &got_packet, -1, -1);

        				av_packet_unref(pkt);
        				av_init_packet(pkt);
        				av_copy_packet(pkt, &convertedPkt);

        				av_packet_unref(&convertedPkt);

        				break;
        			}
        		} else {
        			av_packet_unref(pkt);
                	av_init_packet(pkt);
                    av_copy_packet(pkt, &state->pFormatCtx->streams[i]->attached_pic);
        			
        			got_packet = 1;
        			break;
        		}
        	}
        }
    }

	av_frame_free(&frame);

	if (got_packet) {
		return SUCCESS;
	} else {
		return FAILURE;
	}
}